1

Is there a way to tell when an Android device is in sleep mode? When testing my app, I press the power button to turn off the screen and need to wait until sleep mode is active. I then send messages to my device using Google Cloud Messaging (GCM) and then play a sound and notification once it arrives. But if I send this message before sleep mode is active, then I am not sure whether it got the message because it was still alive or because my service routine got started by the GCM broadcast receiver.

Johann
  • 27,536
  • 39
  • 165
  • 279
  • This might help: http://stackoverflow.com/questions/15620139/when-android-device-goes-to-sleep-mode – Jong Jul 09 '13 at 09:09

4 Answers4

1

You can find out when the screen goes off using android.intent.action.SCREEN_OFF intent but there is no Intent to find out with certainty if the device is in sleep mode as far as I know.

Anup Cowkur
  • 20,443
  • 6
  • 51
  • 84
1

There is no certain method to know if the device is sleeping. But considering that in sleeping mode the device should be running at minimum frequency and and the cpu consumption should be minimal, we can make an educated guess.

To find the current frequency of the device we can use cat /sys/devices/system/cpu/cpu0/cpufreq from adb shell and to find the cpu usage check out this link: Get Memory Usage in Android

I'm not sure though if these queries would themselves wake the device up.

Community
  • 1
  • 1
Srikant Sahay
  • 885
  • 6
  • 9
0

you can not tell in the application level, as when the device is in sleep, the cpu stops working and none of your code is executing.

jiawen
  • 1,198
  • 2
  • 17
  • 29
0

You can use the very informative dumpsys command to look it up.

Here is a one-liner adb command:

adb shell 'if [ -z $(dumpsys power | grep mScreenOn=true) ]; then echo off; else echo ON; fi'

Simon Rigét
  • 2,757
  • 5
  • 30
  • 33