Android – although the permission has been granted, it still obtains the SecurityException

I didn't get a java.lang.securityexception for my app and didn't grant android.permission.write_ Settings. Even if I have added it to the manifest XML file

<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"/>

Edit 1: OK, so I must run this code in a class other than the activity. How will I seek user permission, and then, if they grant this permission, please run this code in an external class. Activity? -

Uri newringtone = Uri.parse("android.resource://com.example.myapp/" + soundID); 
ringtoneManager.setActualDefaultringtoneUri(context,ringtoneManager.TYPE_ringtone,newringtone);

Before someone asks, yes, it must be outside the activity because of the way I set up my application

resolvent:

Per write_ SETTINGS documentation:

Therefore, you should use the following code:

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

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>