In my application with targetSdkVersion as 23, while using the API Settings.System.putString().
Following error is being thrown and app crashes
"AndroidRuntime: java.lang.IllegalArgumentException: You cannot keep your settings in the secure settings."
After trying solution at
Can't get WRITE_SETTINGS permission
and granting the app write permission in screen opened by ACTION_MANAGE_WRITE_SETTINGS. The app still gets the error "You cannot keep your settings in the secure settings".
Is requesting the WRITE_SETTINGS permission now only for apps developed by OEMs ? Is there a solution possible ?
Sharing a sample code , tested on Nexus 5 device with M OS.
android:minSdkVersion="17"
android:targetSdkVersion="23"
uses-permission android:name="android.permission.WRITE_SETTINGS"
protected void onResume() {
super.onResume();
boolean canDo = Settings.System.canWrite(this);
if (false == canDo)
{
Intent grantIntent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
startActivity(grantIntent);
}
else
{
Settings.System.putString(this.getContentResolver(),
"test.hemant", "hemantval");
}
}
E/DatabaseUtils( 779): Writing exception to parcel
E/DatabaseUtils( 779): java.lang.IllegalArgumentException: You cannot keep your settings in the secure settings.
E/DatabaseUtils( 779): at com.android.providers.settings.SettingsProvider.warnOrThrowForUndesiredSecureSettingsMutationForTargetSdk(SettingsProvider.java:1175)
E/DatabaseUtils( 779): at com.android.providers.settings.SettingsProvider.enforceRestrictedSystemSettingsMutationForCallingPackage(SettingsProvider.java:1030)
E/DatabaseUtils( 779): at com.android.providers.settings.SettingsProvider.mutateSystemSetting(SettingsProvider.java:906)
E/DatabaseUtils( 779): at com.android.providers.settings.SettingsProvider.insertSystemSetting(SettingsProvider.java:874)
E/DatabaseUtils( 779): at com.android.providers.settings.SettingsProvider.call(SettingsProvider.java:257)
E/DatabaseUtils( 779): at android.content.ContentProvider$Transport.call(ContentProvider.java:398)
E/DatabaseUtils( 779): at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:283)
E/DatabaseUtils( 779): at android.os.Binder.execTransact(Binder.java:453)
D/AndroidRuntime(19935): Shutting down VM
If you will not be targeting Android 10 (API level 29), some of these changes might not immediately affect you. However, while you can currently use some non-SDK interfaces (depending on your app's target API level), using any non-SDK method or field always carries a high risk of breaking your app.
New apps must target Android 12 (API level 31) or higher; except for Wear OS apps, which must target Android 11 (API level 30) or higher.
Go to: File > Project Structure , then under modules choose your module (that probably will be app , then under the tab flavors you can see minimum sdk and target sdk.
Now you can use Intent intent = new Intent(android. provider. Settings. ACTION_SECURITY_SETTINGS); startActivity(intent); There are whole bunch of constants for every main settings category that you can choose from.
Your code should fail on all versions of Android. If it worked prior to Android 6.0, that was a bug that apparently just got fixed.
Settings.System
is for platform-defined settings. You cannot put arbitrary settings in there, such as test.hemant
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With