未经WRITE_SETTINGS明确许可,设置自定义铃声

时间:2018-06-14 12:48:13

标签: android permissions ringtone ringtonemanager

我想简单地设置铃声。 我不想给WRITE_SETTINGS许可, 我可以找到大部分答案给予WRITE_SETTINGS许可 但有一个应用我正在使用该应用程序没有任何WRITE_SETTINGS权限来设置铃声

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

当您安装此应用时,它永远不会明确地询问权限android.permission.WRITE_SETTINGS(也在marshmallow中)

这是从存储中选择铃声的方法

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);
}

//这里我要设置铃声

        @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);
            }
        }
    }
}

但我收到此错误

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)

请帮忙。 提前谢谢。

2 个答案:

答案 0 :(得分:5)

必须在代码中使用 android.permission.WRITE_SETTINGS并要求用户明确授予此权限以使您的代码/应用程序正常运行。没有WRITE_SETTINGS权限,没有其他人可以做到这一点。

此外,您提到的Google应用程序也使用此WRITE_SETTINGS权限(修改系统设置)。请查看以下屏幕截图以获取更多信息:

enter image description here

如何使用WRITE_SETTINGS检查以下参考资料:

  1. 官方文件:https://developer.android.com/reference/android/Manifest.permission#WRITE_SETTINGS
  2. 关于SO的最佳答案之一:https://stackoverflow.com/a/32083622/631803

答案 1 :(得分:3)

使用方式必须使用android.permission.WRITE_SETTINGS 我只安装了该应用程序,然后查看他的使用方式,我相信两件事

  • 获取铃声的默认URI并重写音频字节流。
  • 请尽可能替换默认铃声