androidadbsleepthread-sleepwakelock

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?


Solution

  • 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!