tags:

views:

145

answers:

1

Is there any way to detect whether an android phone is in sleep mode (screen is black) in the code? I wrote a home screen widget. I don't want the widget gets updated when the screen is black to save the battery consumption.

Thanks.

+1  A: 

You could use the AlarmManager to trigger the refreshes of your widget. When scheduling your next cycle you can define whether to wake up your device (aka perform the actual task).

alarmManager.set(wakeUpType, triggerAtTime, pendingIntent);
Soulreaper
Or, just don't use a `WAKEUP` alarm and stick with `setRepeating()`. If you tell the alarm not to wake up the phone, it won't.
CommonsWare