Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
874 views
in Technique[技术] by (71.8m points)

android - Programmatically enable/disable "Battery Saver" mode

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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
" +
        "am broadcast -a android.os.action.POWER_SAVE_MODE_CHANGED --ez mode true
";
private static String COMMAND_DISABLE = "settings put global low_power 0
" +
        "am broadcast -a android.os.action.POWER_SAVE_MODE_CHANGED --ez mode false
";

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...