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

问题描述 投票:5回答:2

我想简单地设置铃声。我不想给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)

请帮忙。提前致谢。

android permissions ringtone ringtonemanager
2个回答
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

3
投票

你使用它的方式必须使用android.permission.WRITE_SETTINGS我只是安装该应用程序并查看他使用的方式我相信2件事

  • 获取铃声的默认URI并重写音频字节流。
  • 如果可以,只需更换默认铃声即可
© www.soinside.com 2019 - 2024. All rights reserved.