17

I am trying to find if I can enable and/or disable Android's built-in "Battery Saver" mode programmatically.

Any official approaches, or trickery, are welcome.

For reference, here is how to do it following Android's standard UI in 5.0: http://www.androidcentral.com/android-50-lollipop-basics-how-get-more-life-between-charges-battery-saver

I am aware you can detect it -- that is not what I am after.

Thanks all.

Christopher Bull
  • 2,489
  • 1
  • 23
  • 27
  • You can not turn on battery saver mode programmatically. It causes security breaches. Only phone user can change the mode. Applications can not change battery saver mode – mertyildiran Jan 30 '15 at 11:16
  • 2
    Thank you @MehmetMertYidiran. For further reading, do you have any more info/links on how or what "security breaches" could be caused through this? – Christopher Bull Jan 30 '15 at 11:33
  • 2
    "security breaches": I mean, think about that scenario I'm the phone user and I'm playing a game on my phone. Your application running on background and then suddenly change my phone to battery saver mode. All at once, my performance will decrease, my display will darken and perhaps even my apps will crash. Because of that it means a great risk for users and Android do not allow to change battery saver mode programmatically. – mertyildiran Jan 30 '15 at 11:40
  • mertyildiran, What you are describing is the result of power saving mode, not the "Power saving" option for location services. Location services are not going to darken your screen or make your app crash. – pjc May 24 '16 at 19:04

2 Answers2

18

You can enable/disable Battery Saver programmatically on rooted devices. You have to edit the low_power value in global table, in /data/data/com.android.providers.settings/databases/settings.db file.

If your device supports settings command, you can execute (as root):

settings put global low_power 1

to enable Energy Saver and

settings put global low_power 0

to disable it. If it doesn't, use sqlite3 command:

sqlite3 /data/data/com.android.providers.settings/databases/settings.db "update global set value='1' where name='low_power';"
sqlite3 /data/data/com.android.providers.settings/databases/settings.db "update global set value='0' where name='low_power';"

Remember that you have to unplug your phone from PC first, otherwise system will disable Energy Saver. Use ADB over WiFi or Android Terminal (Emulator).

UPDATE:

The sqlite3 method doesn't seem to be reliable anymore.

I'm not sure if android.os.action.POWER_SAVE_MODE_CHANGED broadcast gets send. Maybe you have to send it manually, like in code from here:

private static String COMMAND_ENABLE = "settings put global low_power 1\n" +
        "am broadcast -a android.os.action.POWER_SAVE_MODE_CHANGED --ez mode true\n";
private static String COMMAND_DISABLE = "settings put global low_power 0\n" +
        "am broadcast -a android.os.action.POWER_SAVE_MODE_CHANGED --ez mode false\n";

Also, it's been reported that a new power_saving entry appeared in settings database, however in Android 6.0.1 I haven't managed to find it. In Android 7.1.1_r13 low_power is still used internally (e.g. here), however it may get changed in some Android release. You may want to keep checking changes in e.g. this and this file.

pzmarzly
  • 778
  • 15
  • 29
  • How do I edit the low_power value in global table, in /data/data/com.android.providers.settings/databases/settings.db file? Do you have the code for that? Also, is this answer still relevant? – Rosenpin Jul 20 '16 at 21:49
  • 1
    @Rosenpin I've just checked on Android 6.0.1 it's still working. For Java code to run Linux shell commands on Android, check out f.e. http://stackoverflow.com/a/9194787/5108318 – pzmarzly Jul 20 '16 at 22:18
  • Awesome! Thank you. – Rosenpin Jul 20 '16 at 22:55
  • I also have "power_saving" in settings, so what's the difference between power_saving and low_power. I see that from gui setting menu it only changes power_saving, not low_power. – ransh Jan 11 '17 at 08:57
  • @ransh I've just updated my answer. I've linked files that should be monitored in case API changes, however it shouldn't. – pzmarzly Jan 11 '17 at 17:18
  • Note: There is no such file `databases/settings.db` in that folder anymore in `Android 8`. – Michal Przybylowicz Jul 11 '18 at 09:30
  • does the system gets notified as soon as the global changes (by file monitoring or some other mechanism) and then updates without reboot ? – Talespin_Kit Aug 28 '18 at 13:21
  • 1
    @Talespin_Kit when I wrote this answer 3 years ago, that was the case - it updated instantly – pzmarzly Aug 28 '18 at 17:05
1

You cannot without rooting your phone. I am not sure why this is the case, especially where location services are required now for viewing scan results since SdkVersion 23+ .

This issue is very revealing. https://code.google.com/p/android/issues/detail?id=185370

The best answer is application developers are being forced to crowd source network location for their google overlords. Notice, the OS itself has no problem displaying scan results without location services on.

Revealing that there is no way to turn on location services without GPS where location services are inactive. This two step shuffle is a major quality of experience issue for users. Turn location services on, then turn GPS off.

Watch this space, more lock downs on the way.

pjc
  • 240
  • 5
  • 14