Checking for Power Saver Mode programmatically
Solution 1:
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)
Solution 2:
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.
Solution 3:
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");