1. 简介
MediaScannerService简称MSS, 是一个运行于后台的Service, 实现了Runnable接口.
MediaScannerReceiver接收广播, 然后由MSS具体完成工作. MSS中主要工作在ServiceHandler实现
2. 初始化
2.1 onCreate()
完成2项工作
(1)启动线程
// Start up the thread running the service. Note that we create a
// separate thread because the service normally runs in the process's
// main thread, which we don't want to block.
Thread thr = new Thread(null, this, "MediaScannerService");
thr.start();
之后会调用run方法, 参见2.2
(2) 注册/监听SDCard 卸载事件
IntentFilter filter = new IntentFilter(Intent.ACTION_MEDIA_EJECT);
filter.addDataScheme("file");
filter.setPriority(100);
registerReceiver(mUnmountReceiver, filter);
2.2 run
MSS实现Runable的run方法, onCreate时会调用此方法启动线程.
run方法主要功能就是启动ServiceHandler
public void run()
{
Looper.prepare();
mServiceLooper = Looper.myLooper();
mServiceHandler = new ServiceHandler();
/// M: reduce thread priority after ServiceHandler have been created to avoid cpu starvation
/// which may cause ANR because create service handler too slow.
// reduce priority below other background threads to avoid interfering
// with other services at boot time.
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND + Process.THREAD_PRIORITY_LESS_FAVORABLE);
Looper.loop();
}
2.3 onStartCommand
(1) 等待ServiceHandler启动
while (mServiceHandler == null) {
synchronized (this) {
try {
wait(