我有一个倒计时计时器,当它熄灭(到零)时,它会检查应用程序是否具有焦点.如果不是,它将在通知栏中启动通知.当您单击通知时,将重新打开该应用程序.现在所有这些都可以正常工作,但是如果屏幕碰巧关闭,计时器会继续运行,并且通知会在适当的时间提供,但直到我重新打开屏幕之前,它才不会真正振动或振铃.然后,它显示通知,就像它正在排队一样.
我如何获得它,以便通知管理器在屏幕关闭时实际上会警告用户?
更新:如果我将计时器设置为2分钟,则通知还需要2-3分钟才能真正生效.因此它确实有效,但是延迟很大!
代码:因此,当应用程序失去焦点时,我设置了通知服务,当MyCount1完成时,将检查应用程序是否具有焦点,如果没有,它将显示通知.屏幕背光打开时,所有这些都有效.一旦熄灭,将是不可靠的.
@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);
}
}
}