9

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
Community
  • 1
  • 1
Hemant Tiwari
  • 145
  • 1
  • 1
  • 9
  • 1
    Can you post more code to show how you are using it? – Sharjeel Oct 06 '15 at 14:04
  • "The solutions earlier at Android M 6.0 RingtoneManager - Manifest.permission.WRITE_SETTINGS Error Is not effective" -- please explain what "Is not effective" means. – CommonsWare Oct 06 '15 at 14:06
  • @CommonsWare: Thank you for help. That means after following the steps mentioned in above link 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". – Hemant Tiwari Oct 07 '15 at 04:11
  • OK, so then I'm back with Sharj's comment: you need to provide a [minimal, complete, and verifiable example](http://stackoverflow.com/help/mcve) for us to be able to help you. In this case, this would be the full stack trace, along with the code referred to from that stack trace. – CommonsWare Oct 07 '15 at 11:02
  • @CommonsWare: Added the code with stack trace. – Hemant Tiwari Oct 07 '15 at 12:49
  • @Sharj: Added the code with stack trace. – Hemant Tiwari Oct 07 '15 at 12:49
  • See: https://issuetracker.google.com/issues/37070806 – ballzak May 12 '18 at 02:10

1 Answers1

6

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • yes, It seems this check is added only in Android 6.0. The write operation on standard platform settings is working. – Hemant Tiwari Oct 07 '15 at 17:39
  • @Benny could you explain how to solve this problem? I'm having the same problem. The permission is granted but the error message persist. HTC Smarthphone – Marcello Câmara Nov 05 '19 at 17:22
  • @MarcelloCâmara, My suggestion is to make the app as system app by copy the `.apk` into the `system/priv-app` directory of the device. you need access to do this. and this approach is not good for everyone. – Benny Nov 06 '19 at 07:09