开发者

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

I'm writing an android application that depends on netw开发者_C百科ork 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?


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.


Step 1

a. Put the device to "discharging" in settings (follow instructions in the red boxes).

b. Turn off the device screen by pressing the "power" button (blue box).

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

Step 2

a. To directly put the device to sleep mode, run:

adb shell dumpsys deviceidle force-idle

b. Or slowly step into sleep mode, run:

adb shell dumpsys deviceidle step multiple times.

Step 3

To confirm device idle status, run:

adb shell dumpsys deviceidle get deep


Pressing the power button on the device should put it to sleep. There is also a goToSleep(long time) method in the PowerManager, but that requires DEVICE_POWER permission, which is reserved for system applications.


if u want to test on the emulator then from home screen press menu -> setting -> applications -> development ->uncheck stay awake

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜