Send notification to user even when screen off
My app sends notifications via NotificationManager to the user once every hour. Now it may happen that the screen of the device goes off within that hour, but my notification should still be sent (and ring/vibrate the phone, but not turn on screen). How can I achieve to send a notification even if screen is off (CPU off)? I read about Handler and AlarmManager, but not sure if that's what I need. Please advise. Thanks.开发者_如何学编程
Use AlarmManager
to get control every hour -- whatever you are doing right now is very inefficient.
Specifically, you will need to use a getBroadcast()
PendingIntent
and a _WAKEUP
style alarm. This will wake up the phone and give you control in onReceive()
of your BroadcastReceiver
, with the device awake until onReceive()
returns. There, you raise your Notification
.
精彩评论