Reputation: 23
I need to stop and then start (restart) my MainService when the user plugs in the phone's charger. To do this, I have a so-called GodService that registers a receiver for the ACTION_BATTERY_CHANGED intent, and then I restart MainService within that BroadcastReceiver.
I can post the code to that if necessary, but that seems to work fine, since, as soon as I start debugging I'm charging, and I can step through and see MainService get stopped and restarted.
My question is, how can I test this multiple times in a row (which I need to do), since I'll have to unplug the device, thus stopping my debugging session. I'm sure there must be a way to debug the charging port removal, but I must not be searching with the correct terms.
Upvotes: 0
Views: 428
Reputation: 2234
You can actually simulate the phone disconnect and reconnect via ADB. Here is the relevant code:
Prior to Android 6+:
Disconnect USB
$ adb shell dumpsys battery set usb 0
Re-connect USB
$ adb shell dumpsys battery set usb 1
For Android 6+:
Disconnect USB
$ adb shell dumpsys battery unplug
Re-connect USB
$ adb shell dumpsys battery reset
EDIT:
Found a nice tuturial on how to mock different battery statuses. https://stanfy.com/blog/android-shell-part-1-mocking-battery-status/
Upvotes: 1