Android mediascanner: (3) mediascannerservice

Source: Internet
Author: User

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:

  • The bundle obtained through startid and intent is sent to the work thread through mservicehandler to execute [line #1 ~ 5];
  • In servicehandler's handlemessage (), perform different scanning tasks based on different "filepath", "volume", and "folder" parameters. [Line #6 ~ 11];
  • After the scan is complete, mediascannerservice's mission is complete. You can stop yourself [line #12].
  • You can view the input parameters based on the startup method in mediascannerpolicer:

  • If "filepath" exists, scan a file and call scanfile (). How to scan a single file is described in Android mediascanner (4) scansinglefile of mediascanner.
  • Otherwise, whether it is for the entire volume or a folder scan, it can be attributed to a directory scan: for internal volume, scan "system/Media"; for external volume, scan the entire "/mnt/sdcard". For those with the "folder" parameter, directories [] only contains the path in "folder.
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.