I have to test how my app behaves in doze mode. According to the documentation, I first must make the device think it's unplugged by entering the following command in the terminal:
$ adb shell dumpsys battery unplug
However, nothing happens and it logs:
Can't find service: battery
What should I do?
There is no battery service as the log points out (this may be device specific).
Enter the following command to find existing battery related services:
$ adb shell service list | grep battery
it will result in something like this
$ adb shell service list | grep battery
88 batterymanager: [android.app.IBatteryService]
107 batterystats: [com.android.internal.app.IBatteryStats]
114 batteryproperties: [android.os.IBatteryPropertiesRegistrar]
It makes sense that to manage the battery, you should use the batterymanager.
$ adb shell dumpsys batterymanager
outputs (in case the usb charged is plugged)
Current Battery Service state:
(UPDATES STOPPED -- use 'reset' to restart)
AC powered: false
USB powered: true
then, when you type
$ adb shell dumpsys batterymanager unplug
and run the previous command again, it outputs
Current Battery Service state:
(UPDATES STOPPED -- use 'reset' to restart)
AC powered: false
USB powered: false
Which validates that you should use the batterymanager service instead of battery.
来源:https://stackoverflow.com/questions/39162535/unplugging-the-device-via-adb-cant-find-service