The SD card audio is set to the phone ringtone after deletion, the phone ringtone does not revert to the default problem

Source: Internet
Author: User

1. Android7.0, the MP3 of the memory card is set as a ringtone, after the MP3 is removed, the ringtone in settings does not change, and the call tone does not change.

Cause: New features of android7.0

Google defaults to this design, in the process of selecting a ringtone, the deleted ringtone will be cached, after the ringtone is deleted, play as a cache file

1. Google currently divides the ringtone into actual default ringtone and cache ringtone, which stores the file URI in the XML file as a key in ringtone, which is stored as stream file in ringtone _cache in the resource.

2. When the ringtone is initialized, the ringtone is initialized in the actual default ringtone, which is variable. The default ringtone is not written before the N version.

3. When you set the ringtone, the actual default ringtone and Ringtone_cache are also written.

4. When the bell is ringing, Mediaplayer.java will play the stream resource file in the Ringtone_cache, so the backup ringtone will still sound even if the original music file is deleted.

5. Alarm, pass the bosom friend and so on, principle ibid.

WORKAROUND: If you need to restore the ringtone to the default ringtone after deleting MP3, you can do this:

1. Enter the ringtone selection interface from settings and call Getactualdefaultringtoneuri to get the current ringtone URI. Then we need to judge each time we get the URI, whether the music of that URI still exists. Judging method:

     Public StaticBoolean isringtoneexist (context context, Uri Uri) {if(uri = =NULL) {log.e (TAG,"Check ringtone exist with null uri!"); return false; } Boolean exist=false; Try{assetfiledescriptor FD= Context.getcontentresolver (). Openassetfiledescriptor (URI,"R"); if(FD = =NULL) {exist=false; } Else{fd.close (); exist=true; }        } Catch(FileNotFoundException e) {e.printstacktrace (); exist=false; } Catch(IOException e) {e.printstacktrace (); exist=true; } log.d (TAG, Uri+"is exist"+exist); returnexist; }
     Public Static BooleanIsringtoneexistbyquerydb (context context, Uri Uri) {if(uri = =NULL) {log.e (TAG,"Isringtoneexistbyquerydb--Check ringtone exist with null uri!"); return false; }        Booleanexist =false; Try{cursor cursor=context.getcontentresolver (). Query (URI,NewString[]{mediastore.files.filecolumns.data},NULL,NULL,NULL); if(Cursor! =NULL&&Cursor.movetofirst ()) {                intPath_index =Cursor.getcolumnindexorthrow (MediaStore.Files.FileColumns.DATA); String Path=cursor.getstring (Path_index); LOG.D (TAG,"Isringtoneexistbyquerydb Path =" +path); if(Path! =NULL&& "". Equals (Path) {exist=true; }            }        } Catch(Exception e) {e.printstacktrace (); } log.d (TAG,"Isringtoneexistbyquerydb" + URI + "is exist =" +exist); returnexist; }

Code path:/frameworks/base/media/java/android/media/Ringtonemanager.java

If the URI music does not already exist, set the default URI to the ringtone. Since the default ringtone of 7.0 is no longer written dead, it is mutable (the pro-test is like this, but not specifically how to do it), so we need ourselves to be the first time we read the default URI saved up.

        Private voidSetsettingifnotset (String settingname, Uri Uri,LongrowId) {contentresolver CR=Mcontext.getcontentresolver (); String Existingsettingvalue=Settings.System.getString (CR, Settingname); //if (Textutils.isempty (Existingsettingvalue)) {//final Uri Settinguri = Settings.System.getUriFor (settingname);//final URI Ringtoneuri = Contenturis.withappendedid (URI, rowId);//Ringtonemanager.setactualdefaultringtoneuri (Mcontext,//Ringtonemanager.getdefaulttype (Settinguri), Ringtoneuri);//                ///m:adds log to debug setting ringtones.//if (DEBUG) {//log.v (TAG, "Setsettingifnotset:name="//+ Settingname + ", value=" + rowId);//                }//            }            if(Textutils.isempty (Existingsettingvalue)) {//Set the setting to the given URISettings.System.putString (Mcontext.getcontentresolver (), Settingname, Contentur                Is.withappendedid (URI, rowId). toString ()); ///m:adds log to debug setting ringtones.LOG.V (TAG, "setsettingifnotset:name=" + Settingname + ", value=" +rowId); } Else {                ///m:adds log to debug setting ringtones.LOG.E (TAG, "setsettingifnotset:name=" + Settingname + "with value=" +existingsettingvalue); }        }

Code path:/frameworks/base/media/java/android/media/Mediascanner.java

The Settingsprovider field is defined by us, in Ringtonemanager.java.

    ///M:add for store and get default ringtone @{    /*** m:the key used to store the default ringtone of voice call. * @hide * @internal*/     Public Static FinalString Key_default_ringtone = "Mtk_audioprofile_default_ringtone"; /*** m:the key used to store the default notification sound. * @hide * @internal*/     Public Static FinalString key_default_notification = "Mtk_audioprofile_default_notification"; /*** m:the key used to store the default alarm sound. * @hide*/     Public Static FinalString key_default_alarm = "Mtk_audioprofile_default_alarm"; /// @}
     Public StaticUri Getdefaultringtoneuri (Context context,inttype) {Uri Defaulturi=NULL; String uristring=NULL; Contentresolver Resolver=Context.getcontentresolver (); Switch(type) { Casetype_ringtone:uristring=Settings.System.getString (resolver, key_default_ringtone);  Break;  Casetype_notification:uristring=Settings.System.getString (resolver, key_default_notification);  Break;  Casetype_alarm:uristring=Settings.System.getString (resolver, key_default_alarm);  Break; default: LOG.E (TAG,"Getdefaultringtoneuri with Unsupport type!"); return NULL; } Defaulturi= (Uristring = =NULL?NULL: Uri.parse (uristring)); LOG.D (TAG,"Getdefaultringtoneuri:type =" + Type + ", default URI =" +Defaulturi); returnDefaulturi; }

Then finally, you can judge in Getactualdefaultringtoneuri ()!

    Public StaticUri Getactualdefaultringtoneuri (Context context,inttype) {String setting=Getsettingfortype (type); if(Setting = =NULL)return NULL; FinalString uristring =Settings.System.getStringForUser (Context.getcontentresolver (), setting, Context.getuserid ()); Try{                BooleanIsexist = isringtoneexist (Context, Uri.parse (uristring)) | |isringtoneexistbyquerydb (Context, Uri.parse (uristring)); LOG.D (TAG,"Getactualdefaultringtoneuri isexist =" +isexist); if(Uristring! =NULL&&!isexist) {log.i (TAG,"Get actual default setdefaulturi=" +uristring); Uri Defaulturi=Getdefaultringtoneuri (Context,type);                Setactualdefaultringtoneuri (Context,type,defaulturi); returnDefaulturi; } log.d (TAG,"Getactualdefaultringtoneuri 2222 uristring =" +uristring); }Catch(Exception ex) {LOG.D (TAG, Ex.getmessage ()); } log.i (TAG,"Get actual default ringtone uri=" +uristring); returnUristring! =NULL? Uri.parse (uristring):NULL; }

The SD card audio is set to be deleted after the phone ringtone, and the ringtone does not revert to the default problem

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.