Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Put Android to sleep for testing

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?

like image 580
Phil Avatar asked Feb 06 '17 21:02

Phil


2 Answers

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!

like image 102
Doug Avatar answered Sep 22 '22 04:09

Doug


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.

like image 28
0xAliHn Avatar answered Sep 25 '22 04:09

0xAliHn