How to access Power Saving Mode Programmatically in Android?

孤人 提交于 2019-12-04 22:02:39
sirius

If you want to test the Power Saving Mode (PSM):

The following works with Android Studio 3.4.1 and a Google Pixel 2 XL phone:

$ adb shell dumpsys battery unplug # a charging device cannot enter PSM
$ adb shell settings put global low_power 1 # enter Power Saving Mode (PSM)

This only works on devices that has the standard Power Saving Mode, like Samsung's, but not Huawei's, for example. On some Samsung devices, this setting is stored in global setting keyed psm_switch, as mentioned above.

You can leave PSM by setting low_power to 0 or by enabling charging again with:

$ adb shell dumpsys battery reset

Source: https://developer.android.com/training/monitoring-device-state/doze-standby#testing_doze_and_app_standby

It's bad practice to control the user's device settings from your app.

Rather consider suspending background services when battery levels are low.

Or to notify the user of low battery power and advising the user to switch of unnecessary settings like Bluetooth or WiFi.

Most devices won't have a built-in power-saver API, so you could try to make your own power-saver mode by turning off the bluetooth if it's on, the wi-fi, etc, deactivating location in settings, or checking for other power-eating options enabled.

There are no available API's to do this in android SDK. however You can control/save the power by Turning off Blutooth,Wi-Fi and other unused sevices inside your coding.

ViciDroid

You can't set the power saving mode programmatically. Allowing this would be a bad idea.

On certain Samsung devices you can check if it is enabled:

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

See this for more info: https://stackoverflow.com/a/39296959/3600178

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!