Force an android phone to sleep, in order to test?

Chris Best picture Chris Best · Aug 5, 2010 · Viewed 16.2k times · Source

I'm writing an android application that depends on network activity and the alarm manager sometimes waking the phone up from a sleeping state.

My question is how can I reliably test this on device? Ideally, how can I force the phone into a full-on sleeping state. Failing that, how can I know for sure when the phone has fully gone to sleep?

How do you test your Alarm Manager / Wake Lock / Sleep handling code?

Answer

Force picture Force · Nov 7, 2016

Turn off the screen of the device/emulator and then run the following commands:

adb shell dumpsys battery unplug
adb shell dumpsys deviceidle step

Re-run the second one until you get the message "IDLE". This works on devices running Android 6.0 or newer.