我有一个倒计时计时器,当它关闭(归零)时,它会检查应用程序是否有焦点。如果没有,它会在通知栏中启动通知。当您单击通知时,将重新打开应用程序。现在所有这些工作正常,但如果屏幕碰巧熄灭,计时器会继续运行,并且通知在正确的时间可用,但在我重新打开屏幕之前不会真正振动或响铃。然后它显示通知,就像它在队列中等待一样。
如何获取它以便通知管理器在屏幕关闭时真正提醒用户?
更新:如果我将计时器设置为 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);
}
}
}