does RingtoneManager.setActualDefaultRingtoneUri() work in API 23?

邮差的信 提交于 2019-12-10 21:03:39

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/42808241/does-ringtonemanager-setactualdefaultringtoneuri-work-in-api-23

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!