Android通知管理器无法在屏幕上关闭
作者:互联网
我有一个倒计时计时器,当它熄灭(到零)时,它会检查应用程序是否具有焦点.如果不是,它将在通知栏中启动通知.当您单击通知时,将重新打开该应用程序.现在所有这些都可以正常工作,但是如果屏幕碰巧关闭,计时器会继续运行,并且通知会在适当的时间提供,但直到我重新打开屏幕之前,它才不会真正振动或振铃.然后,它显示通知,就像它正在排队一样.
我如何获得它,以便通知管理器在屏幕关闭时实际上会警告用户?
更新:如果我将计时器设置为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);
}
}
}
解决方法:
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?
当屏幕关闭时,除非有WakeLock,否则CPU将在此后不久停止运行.
这意味着两件事之一:
>您了解所有这些,并持有WakeLock.从用户喜欢他们的设备的角度来看(例如,良好的电池寿命),这可能不是一个好主意.尽管如此,您可能需要握住更牢固的WakeLock,以使屏幕至少保持暗淡.我没有在WakeLock下尝试发出Notification,所以我不确定所有规则是什么.
>您不了解所有这些信息,因此实际上是在设备进入睡眠状态且CPU已关闭时认为计时器正在运行.当CPU重新打开时,计时器将立即关闭.
使用AlarmManager可使您执行基于计时器的事件,从而唤醒设备,并且同时不需要您的代码在内存中徘徊.我不知道您要在这里做什么(从您的描述中看起来很奇怪),但是AlarmManager可能值得研究,以代替您的计时器.
标签:screen-off,notificationmanager,android 来源: https://codeday.me/bug/20191105/1996875.html