Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Settings.System API behaviour in Android API level 23

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 Manifest Snippet :

    android:minSdkVersion="17"  
    android:targetSdkVersion="23"  
    uses-permission android:name="android.permission.WRITE_SETTINGS"

Code Snippet :

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");
    }           

}

Log Stack :

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
like image 759
Hemant Tiwari Avatar asked Oct 06 '15 13:10

Hemant Tiwari


People also ask

What happens if the device is running Android 10+ API 29 +)?

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.

What API level should I use Android?

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.

How do I check Minsdkversion?

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.

How do I get to programmatically in Android settings?

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.


1 Answers

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.

like image 152
CommonsWare Avatar answered Sep 22 '22 16:09

CommonsWare