Tian haili @ csdn
2012-05-19
This article is an analysis and induction by the author, and is presented with a UML diagram (classdi#/ sequence di. Although it comes from the analysis of the android source code, the text will not take a lot of space to list the source code, so when reading this article, it is best to have the android source code at hand, combined with the source code for interpretation. This article performs static analysis on the class structure of mediascannerservice and dynamically analyzes the work at creation and startup. The analysis process shows how mediascannerservice handles various scanning requests received by mediascannerreceiver.
I. Static STRUCTURE ANALYSIS OF mediascannerservice
- Mediascannerservice is a service that implements runnable and working threads.
- Mediascannerservice uses servicehandler as the handler to put a lot of computing work on the main thread in the working thread.
Execute the message loop in runnable. Run () and execute the message sent by handler in the work thread.
Ii. Dynamic Analysis of mediascannerservice
When mediascannerservice is started through startservice, it actually involves the work during creation. The following describes the work completed at creation and startup respectively.
2.1 creation time #0 ncreate ()
When a service object is created, oncreate () will be called. Let's take a look at what is done in oncreate () of mediascannerservice:
PowerManager pm = (PowerManager)getSystemService(Context.POWER_SERVICE);mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,TAG); Thread thr = new Thread(null, this, “MediaScannerService”);Thr.start();
Mediascannerservice is created to scan media. This process is time-consuming and laborious. Therefore:
- In order to prevent the CPU from sleep during media scanning, use the wakelock of powermanager to tell powermanager that I am still busy. [Line #1, 2];
- In the main thread of Android, a quick response is required. A large number of computing tasks are handed over to the working thread. A working thread is started here, the execution body of this thread is the runnable run () method implemented by mediascannerservice. Before sending messages using handler, you must first start the [line #] of this thread.
2.2 when the service is started # onstartcommand ()
When the service object is started, onstartcommand () will be called to check what is done in onstartcommand () of mediascannerservice.
Is the work completed in mediascannerservice # onstartcommand:
Iii. Summary
This article analyzes the class structure of mediascannerservice statically, dynamically analyzes the work at creation time and startup time, and analyzes how mediascannerservice responds to various scanning requests received by mediascannerreceiver.
Relationships with other articles:
- Looking forward to mediascannerreceiver, scanning requests from external sources, start mediascannerservice, which is specifically implemented by the service;
- Looking back, mediascanner is the real scan implementation, which is explained by Scanning files and scan paths.