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.<
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.
"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
It did not work for me on my Samsung S8, Android 9, instead I used:
$adb shell settings put global low_power 1
$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");
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"));