Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting a custom ringtone without explicit permission of WRITE_SETTINGS

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.

like image 307
Himanshu itmca Avatar asked Jun 14 '18 12:06

Himanshu itmca


2 Answers

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://stackoverflow.com/a/32083622/631803
like image 193
Vikasdeep Singh Avatar answered Sep 22 '22 16:09

Vikasdeep Singh


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
like image 23
A7madKazimi Avatar answered Sep 22 '22 16:09

A7madKazimi