I have a count down timer that when it goes off (to zero) it checks to see if the app has focus. If not it launches a notification in the notification bar. When you click on the notification is re-opens the app. 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?
Update: If I set the timer for 2 minutes, it takes another 2-3 minutes for the notification to actually work. So it does work but it's on a huge delay!
Code: So I setup the notification service when the app loses focus, and when the MyCount1 is finished is checks if the app has focus and if not it shows the notification. This all works when the screen backlight is on. Once it goes off it is unreliable.
@Override
public void onWindowFocusChanged(boolean hasFocus){
if(hasFocus == false){
mFocusFlag = false;
ns = Context.NOTIFICATION_SERVICE;
mNotificationManager = (NotificationManager) getSystemService(ns);
icon = R.drawable.statusbar;
tickerText = "Check the timer!!!";
when = System.currentTimeMillis();
notification = new Notification(icon, tickerText, when);
context = getApplicationContext();
contentTitle = "Countdown Timer";
contentText = "Click to Check the Timer";
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationIntent = new Intent(this, StartTimer.class);
contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
}else{
mFocusFlag = true;
}
}
public class MyCount1 extends CountDownTimer {
public MyCount1(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
public void onFinish() {
if(mFocusFlag == false){
mNotificationManager.notify(HELLO_ID, notification);
}else{
mVib.vibrate(1000);
}
}
public void onTick(long millisUntilFinished) {
if((millisUntilFinished/1000%60) < 10){
mTime.setText("1st Side = " + millisUntilFinished/60000 + ":0" + (millisUntilFinished / 1000)%60);
}else{
mTime.setText("1st Side = " + millisUntilFinished/60000 + ":" + (millisUntilFinished / 1000)%60);
}
}
}
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:
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 strongerWakeLock
, one that keeps the screen at least dim. I have not tried raising aNotification
while under aWakeLock
, so I am uncertain what the rules all are.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.
来源:https://stackoverflow.com/questions/4169558/android-notification-manager-doesnt-work-with-the-screen-is-off