Unplugging the device via ADB: “can't find ser

2019-04-16 02:24发布

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?

1条回答
forever°为你锁心
2楼-- · 2019-04-16 03:07

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.

查看更多
登录 后发表回答