Realize mobile phone ringtone, notification ringtone, warning ringtone and other audio customization functions (one, add scan partition MyImage)

Source: Internet
Author: User

Requirements Function Description: This customization requirement is produced in the context of national differences in various countries, and requires adding a new partition such as MyImage in the system to enable the storage of customized resources to meet the experience of users in different countries. For example, the new Media folder under MyImage is used to store customized audio resources, if the system myimage partition, the relevant directory or file exists in the media directory, the system will discard the system name of the default resource, preference for the custom resources under the partition.
Note!
This blog post combines the system to analyze and modify the scanning process of multimedia files after the system boot is completed. The idea is: Prioritize scan custom partitions to customize the audio resources and insert the scan results into the corresponding database. If the resource directory or file does not exist under the custom partition, the scan result of the system default resource is inserted into the corresponding database, and if the custom resource file name is the same as the system resource file name, the custom resource is inserted preferentially.
Reference Code:ANDROID4.1.2_R21. When the system is started, Mediascannerreceiver receives the broadcast that the system started to complete, and calls the scan () method to scan the built-in storage device. Related classes: Mediascannerreceiver.java is a broadcastreceiver, receives the broadcast, carries on the media scanning, is the Mediascanner provides the interface, mainly is responsible for receives the broadcast and initiates the Mediascannerservice specific execution scan Job. method Invocation Flow: Onreceiver ()-->scan ()--scandirectories () The specific code is as follows:Public voidonreceive(context context, Intent Intent) {...... if (action.equals (intent.action_boot_completed)) {//Receive a broadcast for system boot completion
//Scan internal storage device, Internal_volume value is "INTERNAL"
Scan (context, mediaprovider.internal_volume);
}......} private voidScan(context context, String volume) {
Bundle args = new bundle ();
Args.putstring ("Volume", volume); //Start the internal media file scan via StartService () Start service, where the value of the parameter volume is "internal".
Context.startservice (New Intent (context, Mediascannerservice.class). Putextras (args));
}2.The Mediascannerservice service is started and is primarily responsible for media scanning. In more detail , Mediascannerservice is used to receive messages such as after the system restarts, plug-and-Unplug SD cards, file copy and copy, and when the message is received, a handler is started to process the message and scan. Finally, the scan results will be placed in the database provided by Mediaprovider. Here we start with the OnCreate life cycle function analysis. The main methods are as follows:handlemessage ()-->scan ( )
Public voidonCreate() {powermanager pm = (powermanager) getsystemservice (context.power_service); //Keep the screen solid during scanning
Mwakelock = Pm.newwakelock (Powermanager.partial_wake_lock, TAG);
......Start a worker thread to handle a lot of computational tasks, because Mediascannerservice implements Runnable, so a lot of computational tasks are performed in the runnable run () method.
Thread thr = new Thread (NULL, this, "Mediascannerservice");
Thr.start ();     ......}A message loop is executed in Runnable.run (), and messages sent over handler are executed in the worker thread. Public voidRun() { process.setthreadpriority (Process.thread_priority_background + process.thread_priority_less_favorabl E);
Looper.prepare ();
Mservicelooper = Looper.mylooper ();
Mservicehandler = new Servicehandler ();
Looper.loop ();
}The //service object is invoked when it is started, and Onstartcommand () is called. public intOnstartcommand(Intent Intent, int flags, int startid) {
....
Message msg = Mservicehandler.obtainmessage ();
MSG.ARG1 = Startid;
Msg.obj = Intent.getextras ();
Mservicehandler.sendmessage (msg);
return service.start_redeliver_intent;
}Private Final classServicehandlerextends Handler
{
@Override
public void handlemessage(Message msg) {             ......
Bundle arguments = (bundle) Msg.obj;
String FilePath = arguments.getstring ("FilePath");
String volume = arguments.getstring ("volume");
string[] directories = null;                    //According to the previous article we know that the value of the parameter volume here is "internal", so the "System/media" path is scanned by default.
                    if (MediaProvider.INTERNAL_VOLUME.equals (VOLUME ) {
                         directories = new String [] {
                                &N Bsp Environment.getrootdirectory () + "/media", Note: As per demand here we have added a new partition such as MyImage to store the resources we need to customize, so here we add a scan path to the internal scan, that is, "Myimage/media".
"Myimage/media"
};
}if (directories! = null) {
......
Scan (directories, volume);
......
}
        ......}//Get a contentresover from Getcontentresolver and then insert directly according to Aidl, the other end of this contentresover is Mediaprovider. A scan URI is mainly obtained here. private voidscan(string[] directories, String volumename) {
Contentvalues values = new Contentvalues ();
Values.put (Mediastore.media_scanner_volume, volumename);
Uri Scanuri = Getcontentresolver (). Insert (Mediastore.getmediascanneruri (), values); //Do not make any changes here to keep the original database structure unchanged.
Uri uri = uri.parse ("file://" + directories[0]);        //Send notification to start scanning
         sendbroadcast (new Intent (intent.action_media_scanner_started, uri ));
         try {
             if (Volumename.equals (Media Provider.external_volume) {
                 opendatabase (volumename);
             }
             mediascanner scanner = Createmediascanner (); Scan Directory
Scanner. scandirectories (directories, VolumeName);
} catch (Exception e) {
LOG.E (TAG, "exception in Mediascanner.scan ()", e);
} //delete scan path to show the end of the scan (this is because the time problem is not verified, presumably there will be a place to capture the deletion action, and broadcast to notify the end of the scan)
Getcontentresolver (). Delete (Scanuri, NULL, NULL);
Sendbroadcast (New Intent (intent.action_media_scanner_finished, URI));
Mwakelock.release (); }

Realize mobile phone ringtone, notification ringtone, warning ringtone and other audio customization functions (one, add scan partition MyImage)

Related Article

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.