Force an android phone to sleep, in order to test?
Asked Answered
A

4

25

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?

Abolition answered 5/8, 2010 at 17:15 Comment(0)
C
12

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.

Chiccory answered 7/11, 2016 at 12:49 Comment(0)
V
2

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).

Turn of the phone in Emulator

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

Valued answered 17/12, 2017 at 0:59 Comment(0)
C
1

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.

Criseyde answered 5/8, 2010 at 18:2 Comment(6)
Thanks. This is what I have been doing, but I suspect that pressing power puts the display to sleep immediately, but does not necessarily force the CPU to sleep (which apparently can eventually happen - judging by this developer.android.com/reference/android/app/…)Abolition
Hmm, I thought power did this. In the debug log I'm used to seeing a message like "I'm tired, going to sleep" when I press the power button, so I'm pretty sure it is going to sleep / scaling back the processor.Engedi
Maybe this is a per-device thing. I'm using a Milestone, and it doesn't say that for me.. the screen just turns off, and i keep getting log messages as usual.Abolition
You need to disconnect form USB to see true sleep effects on your app. Leaving USB plugged in through Eclipse does strange things with background services.Galling
of course what @Ryan Conrad states is nonsense - this is what wakelock is for - if some app acquires it the device will not go asleep when power button is pressed, see software.intel.com/en-us/android/articles/…Fencing
@MarianPaździoch you realize the article you linked to was written 3 years after I posted this answer. And, how is it nonsense? Pressing the power button does put it to sleep. The question was "how do i force the phone in to a full sleep state", wakelock keeps it awake, the opposite of what the original question was.Criseyde
D
-4

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

Degust answered 20/11, 2010 at 6:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.