24

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?

molnarm
  • 9,585
  • 2
  • 41
  • 55
Chris Best
  • 241
  • 1
  • 2
  • 3

4 Answers4

11

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.

Force
  • 5,986
  • 6
  • 50
  • 81
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

user1032613
  • 17,258
  • 13
  • 73
  • 91
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.

Ryan Conrad
  • 6,800
  • 2
  • 33
  • 36
  • 5
    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 http://developer.android.com/reference/android/app/AlarmManager.html#RTC_WAKEUP) – Chris Best Aug 05 '10 at 18:19
  • 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. – Brad Hein Aug 05 '10 at 18:28
  • 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. – Chris Best Aug 05 '10 at 19:36
  • 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. – just_another_coder Sep 01 '10 at 16:42
  • 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 https://software.intel.com/en-us/android/articles/wakelocks-detect-no-sleep-issues-in-android-applications – Marian Paździoch Oct 07 '15 at 06:55
  • @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. – Ryan Conrad Oct 07 '15 at 13:01
-4

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

bhanu
  • 9
  • 3