Setting a custom ringtone without explicit permission of WRITE_SETTINGS
Asked Answered
O

2

6

I simple want to set the ringtone. I don't want to give permission of WRITE_SETTINGS, i can find most of the answer to give permission of WRITE_SETTINGS but there is an app i am using that app has not any WRITE_SETTINGS permission to set the ringtone

https://play.google.com/store/apps/details?id=com.atomic.apps.ringtone.cutter

When you install this app it never ask permission explicitly android.permission.WRITE_SETTINGS (also in marshmallow)

this is the method to pick the ringtone from storage

public void pickRingtone()
{
    Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
    intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_RINGTONE);
    intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "Select Ringtone");
    Uri urie = RingtoneManager.getActualDefaultRingtoneUri(getApplicationContext(), RingtoneManager.TYPE_RINGTONE);
    intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, urie);
    startActivityForResult(intent, REQUEST_CODE);
}

// here i am going to set the ringtone

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) 
        {
if (requestCode == REQUEST_CODE) {
        if (resultCode == RESULT_OK) 
        {
            Uri uri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
            if (uri != null)
            {
                String ringTonePath = uri.toString();
                RingtoneManager.setActualDefaultRingtoneUri(context, RingtoneManager.TYPE_RINGTONE, uri);
            }
        }
    }
}

but i am getting this error

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=5, result=-1, data=Intent { dat=content://com.mi.android.globalFileexplorer.myprovider/external_files/Download/Amazing Amitabh Bachchan Voice  Motivational pink film poem Tu khud ki khoj me nikal (192  kbps).mp3 flg=0x1 (has extras) }} to activity {com.example.himanshu.defaultringtone/com.example.himanshu.defaultringtone.MainActivity}: java.lang.SecurityException: com.example.himanshu.defaultringtone was not granted  this permission: android.permission.WRITE_SETTINGS.
                at android.app.ActivityThread.deliverResults(ActivityThread.java:4162)
                at android.app.ActivityThread.handleSendResult(ActivityThread.java:4205)
                at android.app.ActivityThread.-wrap20(ActivityThread.java)
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1572)
                at android.os.Handler.dispatchMessage(Handler.java:102)
                at android.os.Looper.loop(Looper.java:163)
                at android.app.ActivityThread.main(ActivityThread.java:6221)
                at java.lang.reflect.Method.invoke(Native Method)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
             Caused by: java.lang.SecurityException: com.example.himanshu.defaultringtone was not granted  this permission: android.permission.WRITE_SETTINGS.
                at android.os.Parcel.readException(Parcel.java:1684)
                at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183)
                at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
                at android.content.ContentProviderProxy.call(ContentProviderNative.java:646)
                at android.provider.Settings$NameValueCache.putStringForUser(Settings.java:1601)
                at android.provider.Settings$System.putStringForUser(Settings.java:1992)
                at android.media.RingtoneManager.setActualDefaultRingtoneUri(RingtoneManager.java:666)
                at com.example.himanshu.defaultringtone.MainActivity.onActivityResult(MainActivity.java:77)

Please help. Thanks in advance.

Obumbrate answered 14/6, 2018 at 12:48 Comment(0)
B
6

You must have to use android.permission.WRITE_SETTINGS in your code and ask user to explicitly grant this permission to make your code/application work. There is no other to do that without the use of WRITE_SETTINGS permission.

Also the application from Google you have mentioned is also using this WRITE_SETTINGS permission (modify system settings). Check the below screenshot for more information:

enter image description here

For how to use WRITE_SETTINGS check below references:

  1. Official documentation : https://developer.android.com/reference/android/Manifest.permission#WRITE_SETTINGS
  2. One of the best answer on SO : https://mcmap.net/q/223936/-can-39-t-get-write_settings-permission
Buchholz answered 16/6, 2018 at 16:4 Comment(0)
M
3

The way you use you must have to use android.permission.WRITE_SETTINGS I just install that app and looking the way he uses I believe in 2 things

  • get the default URI for ringtone and rewrite the audio byte stream.
  • Just replace the default ringtone if you can
Microcyte answered 20/6, 2018 at 21:40 Comment(1)
I think it can be helpful, Now i have the byte stream of my new ringtone and the URI of default ringtone, so can you tell me how to replace the exact URI??Obumbrate

© 2022 - 2024 — McMap. All rights reserved.