Android notification manager doesn't work with the screen is off

怎甘沉沦 提交于 2019-12-04 13:23:01

Now all of this works fine but if the screen happens to go off, the timer keeps going and the notification is available at the right time but never actually vibrates or rings until i turn the screen back on. Then it displays the notification like it was waiting in a queue or something.

How do I get it so that the notification manager will actually alert the user when the screen is turned off?

When the screen turns off, the CPU will stop running shortly thereafter, unless something is holding a WakeLock.

This means one of two things:

  1. You understand all of this and are holding a WakeLock. This may or may not be a good idea, from the standpoint of what users like with their devices (e.g., good battery life). Be that as it may, you may need to hold a stronger WakeLock, one that keeps the screen at least dim. I have not tried raising a Notification while under a WakeLock, so I am uncertain what the rules all are.

  2. You do not understand all of this, and therefore are thinking your timer is going when, in reality, the device has fallen asleep and the CPU has turned off. When the CPU turns back on again, your timer will go off immediately.

Using AlarmManager allows you to do timer-based events that wake up the device, and do not require your code to be hanging around in memory in the meantime. I have no idea what you're trying to do here (seems rather bizarre from your description), but AlarmManager may be something worth investigating as a replacement for your timer.

It probably goes off when the phone is woken up by some other application, for example email application periodically goes to check for new emails. You need to set your own alarm to fire at the time you need. http://developer.android.com/reference/android/app/AlarmManager.html

NotificationManager doesn't have notion of time. It has to be triggered somehow. The problem in your case is that, if you let the phone sleep, it will do just that. Alarms are specifically exist so you can wake up the device when you need to.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!