android-尽管已授予权限,但仍获得securityexception

我没有为我的应用获取java.lang.SecurityException,而未授予android.permission.WRITE_SETTINGS.即使我已将其添加到清单xml文件中

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/Theme.AppCompat.Light.NoActionBar">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".SoundActivity1"></activity>
</application>

<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>

编辑1:好的,所以我必须在活动之外的类中运行此代码,我将如何寻求用户许可,然后,如果他们授予了该权限,请在外部的类中运行此代码.活动? —

Uri newRingtone = Uri.parse("android.resource://com.example.myapp/" + soundID); 
RingtoneManager.setActualDefaultRingtoneUri(context,RingtoneManager.TYPE_RINGTONE,newRingtone);

在有人问之前,是的,它必须在活动之外,因为我设置我的应用程序的方式

解决方法:

WRITE_SETTINGS documentation

Note: 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 07001. The app can check whether it has this authorization by calling 07002.

因此,您应该使用以下代码:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M &&
    !Settings.System.canWrite(this)) {
  Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
  intent.setData("package:"+getPackageName());
  startActivityForResult(intent);
  // now wait for onActivityResult()
}
上一篇:Cupid's Arrow---hdu1756(判断点与多边形的位置关系 模板)


下一篇:MIME Types