简体   繁体   中英

Checking for Power Saver Mode programmatically

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.

PowerManager powerManager = (PowerManager)getSystemService(Context.POWER_SERVICE);
boolean powerSaveMode = powerManager.isPowerSaveMode();

Docs: developer.android.com/.../PowerManager#isPowerSaveMode()

Added in API level 21 (Android 5.0)

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(){
  @Override
  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.

It did not work for me on my Samsung S8, Android 9, instead I used:

  • to set: $adb shell settings put global low_power 1
  • to get: $adb shell settings get global low_power

...and it worked!

Translating it into java it goes something like this:

final String result = Settings.Global.getString(getContentResolver(),"low_power");

"power saving mode" is not an official AOSP feature and there are no ways to detect it via official SDK. it is manufacturer specific. check this link

This answer is only for the Samsung power saver mode, since as usual Samsung still chooses to mess with the standard APIs.

iKeirNez posted an excellent answer that confirmed a suspicion of mine. This still happens on lower end devices you can buy that have 5.1.1. I have a list compiled of the devices which I know of that suffer from a camera crash when on power saving mode, but I have not yet checked for all the possible keys that relate to power saving mode.

I know about these ones so far:

content://settings/system/powersaving_switch

content://settings/system/psm_switch

You can register a ContentResolver and listen for changes OR you can check it when necessary:

final String result = Settings.System.getString(getContentResolver(), "psm_switch");
Log.v("Debug", "Powersaving active: " + TextUtils.equals(result, "1"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM