Android通知管理器无法在屏幕上关闭 [英] Android notification manager doesn't work with the screen is off

查看:180
本文介绍了Android通知管理器无法在屏幕上关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个倒数计时器,当它熄灭(为零)时,它会检查应用程序是否具有焦点.如果不是,它将在通知栏中启动通知.当您单击通知时,将重新打开该应用程序.现在所有这些都可以正常工作,但是如果屏幕碰巧关闭,计时器会继续运行,并且通知会在适当的时间可用,但直到我重新打开屏幕之前,它才不会真正振动或振铃.然后,它会显示通知,就像它正在排队一样.

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?

更新:如果我将计时器设置为2分钟,则通知还需要2-3分钟才能真正生效.因此它确实可以工作,但是延迟很大!

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!

代码:因此,当应用失去焦点时,并且当MyCount1完成时,我将设置通知服务,以检查应用是否具有焦点,如果没有,则显示通知.屏幕背光打开时,所有这些都有效.一旦熄灭,将是不可靠的.

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?

当屏幕关闭时,除非有东西按住WakeLock,否则CPU将在此后不久停止运行.

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

这意味着两件事之一:

  1. 您了解所有这些内容并持有WakeLock.从用户喜欢他们的设备的角度来看(例如,良好的电池寿命),这可能不是一个好主意.如此一来,您可能需要按住一个更强的WakeLock,使屏幕至少变暗.我没有尝试在WakeLock下时提高Notification,所以我不确定所有规则是什么.

  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.

您不了解所有这些内容,因此实际上是在设备进入睡眠状态并且CPU已关闭的情况下认为计时器正在运行.当CPU重新打开时,您的计时器将立即关闭.

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.

使用AlarmManager,您可以执行基于计时器的事件以唤醒设备,同时不需要您的代码在内存中徘徊.我不知道您要在这里做什么(从您的描述看来很奇怪),但是AlarmManager可能值得研究,以代替您的计时器.

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.

这篇关于Android通知管理器无法在屏幕上关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆