iKeirNez iKeirNez - 1 year ago 167
Java Question

Checking for Power Saver Mode Programically

I have written an app (AutoWifiSwitch) and one of the features I plan to add is automatically disabling the wifi scanning service in my app if power saving mode is enabled.

I know Android L is supposed to have Battery Saving implemented (previously HTC and Samsung would add the features themselves to the software). Presumably this now means Google will have added some sort of API for it. Ideally there would be a new action added so I could listen for that.

I would also like to know if the above is possible with HTC/Samsung APIs and if so, how do I use them.

I've been searching everywhere for the above questions but had absolutely no luck, the app SecureSettings (an addon for Tasker) is able to hook into the HTC/Samsung APIs to enable the power saver anyway, I'm not quite sure how they do it.

Edit: The power saver value can be gotten from the PowerManager in Android L, not sure if there is an Action for it though.

Answer Source

I've eventually figured out how to do this with HTC and Samsung devices. Both store their power manager settings in Settings.System.

HTC (Sense) uses the key user_powersaver_enable. Samsung (Touchwiz) uses the key psm_switch.

Both store the boolean as a String, "0" being false and "1" being true. You can then listen for changes using a ContentObserver like so (requires API level 16 or higher):

getContentResolver().registerContentObserver(Settings.System.CONTENT_URI, true, new ContentObserver(){
  public void onChange(boolean selfChange, Uri uri){
    super.onChange(selfChange, uri);
    String key = uri.getPath();
    key = key.substring(key.lastIndexOf("/") + 1, key.length());

    if (key.equals("user_powersaver_enable") || key.equals("psm_switch")){
      boolean batterySaverEnabled = Settings.System.getString(getContentResolver(), key).equals("1");
      // do something

However this will only be applicable until Android L is release, when L is released HTC and Samsung will likely move over to the AOSP battery saver which means you will be able to use the new battery saver api in L.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download