lazyguy
lazyguy

Reputation: 963

Setting the stay awake while plugged in option programatically in android

I am trying to set the "Stay Awake" check box under Developer Options in Settings in Android programatically.

I am using following code

Settings.Global.putInt(this.getContentResolver(),Settings.Global.STAY_ON_WHILE_PLUGGED_IN, 0);  

and I have following two permissions added in my manifest file.

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

I am using android 4.2.2 to compile the project in eclipse and then running on my tablet which is having Android 4.1.2

When I run the apk crashes and I see this exception in Logcat..

E/AndroidRuntime(6596): java.lang.NoClassDefFoundError: android.provider.Settings$Global

Any suggestions what is going wrong or how to achieve the above functionality? Many thanks!

Upvotes: 3

Views: 2217

Answers (1)

lazyguy
lazyguy

Reputation: 963

Ok Just found the answer for this. I was compiling Settings.Global in Android 4.2.2 but on actual device it was 4.1.2 and looks like Android just moved Stay_ON_WHILE_PLUGGED_IN from System to Global after api level 16. so System will work in API level 16 but Global will work afterwards.

Here is the summary below. Hope this helps someone!

if (currentapiVersion <= 16)
{
    Settings.System.putInt(this.getContentResolver(),Settings.System.STAY_ON_WHILE_PLUGGED_IN, BatteryManager.BATTERY_PLUGGED_USB);
}
else
{   
  Settings.Global.putInt(this.getContentResolver(),Settings.Global.STAY_ON_WHILE_PLUGGED_IN, BatteryManager.BATTERY_PLUGGED_USB);
}

Upvotes: 8

Related Questions