does RingtoneManager.setActualDefaultRingtoneUri() work in API 23?
Asked Answered
S

1

6

This is a simple question, and probably a simple answer but there is a huge amount of context.

The Question: does setActualDefaultRingtoneUri() still work in API 23? because I can't get it to function

The Context: I've set up AndroidManifest.xml with

<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

The app self assigns the permissions with this code

public void desirePermissionCode()
{
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !Settings.System.canWrite(this)) {
        new AlertDialog.Builder(this)
                .setMessage("Please Assign Meep Meep Write Permissions")
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent(android.provider.Settings.ACTION_MANAGE_WRITE_SETTINGS);
                        intent.setData(Uri.parse("package:" + getPackageName()));
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                        try {
                            startActivity(intent);
                        } catch (Exception e) {
                            Log.e("MainActivity", "error starting permission intent", e);
                        }
                    }
                })
                .show();
        return;
    }
}

I then have a simple 2 button demo: one with this (doesn't work)

Uri uri = Uri.parse("android.resource://" + getPackageName() + "/raw/meepmeep");
grantUriPermission("com.android.systemui", uri,
                   Intent.FLAG_GRANT_READ_URI_PERMISSION);
RingtoneManager.setActualDefaultRingtoneUri(
          MainActivity.this,
          RingtoneManager.TYPE_RINGTONE,
          uri
 );

and one with this (does work)

MediaPlayer mpintro;
mpintro = MediaPlayer.create(me, Uri.parse("android.resource://"+getPackageName()+"/raw/meepmeep"));
mpintro.start();

can someone explain to me why, when the 2 permissions are added, and the meepmeep.mp3 is in the res folder, so why does the event fire to play the sound in the app but not assign the ringtone in RingtoneManager.setActualDefaultRingtoneUri

Socha answered 15/3, 2017 at 11:18 Comment(0)
G
2

Please refer to this link:

https://developer.android.com/reference/android/Manifest.permission.html#WRITE_SETTINGS

If the app targets API level 23 or higher, the app user must explicitly grant this permission to the app through a permission management screen. The app requests the user's approval by sending an intent with action ACTION_MANAGE_WRITE_SETTINGS. The app can check whether it has this authorization by calling Settings.System.canWrite().

Please do not use startActivity(intent), instead please use startActivityForResult to listen to the feedback of MANAGE_WRITE_SETTINGS activity. In onActivityResult method, you can check the request code and check Settings.System.canWrite again, if you have write permission now, then you need to set the ringtone again, this is not automatic.

Gaffney answered 14/6, 2017 at 6:48 Comment(3)
yes the example code clearly shows me doing this, thank you, but it doesn't workSocha
@MrHeelis please do not use startActivity(intent), instead please use startActivityForResult to listen to the feedback of MANAGE_WRITE_SETTINGS activity. In onActivityResult method, you can check the request code and check Settings.System.canWrite again, if you have write permission now, then you need to set the ringtone again, this is not automatic.Gaffney
@MrHeelis btw, could you try to set ringtone from phone external storage instead of raw resources? I am not sure if this is the problem.Gaffney

© 2022 - 2024 — McMap. All rights reserved.