14

I'm creating an alarm app and currently have issues dealing with the Wakelock (or perhaps it's something else), to make the alarms work when the phone is asleep. However, my question isn't actually about the Wakelock, but rather about how to make the phone go to sleep so I can test the various options I want to implement on my alarm app. Currently I have to wait for the phone to go to sleep before I can test my code. So is there a way to put the phone in deep sleep programmatically?

Martin Zeitler
  • 1
  • 19
  • 155
  • 216
Phil
  • 305
  • 1
  • 3
  • 13

2 Answers2

18

I ran into the same question during my dev work. The android docs are not very useful -- use adb to switch into doze mode (deep sleep):

adb shell dumpsys deviceidle force-idle

and equally useful, use:

adb shell dumpsys deviceidle step

or

adb shell dumpsys deviceidle unforce

to get back to "normal" mode. Brief intro:

adb shell dumpsys -l  #(lower case "L") list subsystems (deviceidle is one)

then:

adb shell dumpsys deviceidle -h  #parameters with brief description

Also, adb needs access to your device (or emulator) to get this information. Good luck!

Magnus
  • 17,157
  • 19
  • 104
  • 189
Doug
  • 204
  • 2
  • 2
  • Looks like you found it! but where do you put that command line? I can't see the ARGs lines in android! – Phil Feb 16 '17 at 14:30
  • Hi @Phil, **adb** goes in your debug computer's terminal. So, if you have **adb** installed (it comes with the Android SDK, check the path, it may not be in your search PATH) and a debug-enabled phone connected via USB, then you should be fine (it defaults to the only device, if there's only one). **adb** operations happen in parallel to your debug session. Many more options: try **adb -h** for much more detail. Does that answer your question? – Doug Feb 20 '17 at 23:29
  • 2
    but screen display is still on. – ransh Apr 12 '18 at 16:51
  • 6
    i did this but nothing happened, not even the screen went of... how can i know it really worked – Rafael Lima Apr 21 '19 at 20:45
9

You can test Doze mode/Deep Sleep mode by following these steps:

  1. Configure a hardware device or virtual device with an Android 6.0 (API level 23) or higher system image.
  2. Connect the device to your development machine and install your app.
  3. Run your app and leave it active.
  4. Force the system into idle mode by running the following command:

    $ adb shell dumpsys deviceidle force-idle

  5. When ready, exit idle mode by running the following command:

    $ adb shell dumpsys deviceidle unforce

  6. Observe the behavior of your app after you reactivate the device.

0xAliHn
  • 18,390
  • 23
  • 91
  • 111