Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

does RingtoneManager.setActualDefaultRingtoneUri() work in API 23?

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

like image 349
Mr Heelis Avatar asked Oct 29 '22 10:10

Mr Heelis


1 Answers

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.

like image 178
Hexise Avatar answered Nov 03 '22 00:11

Hexise