我想简单地设置铃声。我不想给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.permission.WRITE_SETTINGS
并要求用户明确授予此权限以使您的代码/应用程序正常工作。没有使用WRITE_SETTINGS
许可,没有别的办法。
此外,您提到的Google应用程序也使用此WRITE_SETTINGS
权限(修改系统设置)。查看以下屏幕截图以获取更多信息:
有关如何使用WRITE_SETTINGS
检查以下参考:
你使用它的方式必须使用android.permission.WRITE_SETTINGS
我只是安装该应用程序并查看他使用的方式我相信2件事