Android 通知管理器在屏幕关闭时不起作用

发布于 2024-10-02 08:33:42 字数 2292 浏览 10 评论 0原文

我有一个倒计时器,当它关闭(到零)时,它会检查应用程序是否具有焦点。如果没有,它会在通知栏中启动通知。当您单击通知时,将重新打开应用程序。现在所有这些都工作正常,但如果屏幕碰巧关闭,计时器会继续运行,并且通知会在正确的时间可用,但在我重新打开屏幕之前永远不会真正振动或响起。然后它会显示通知,就像它在队列中等待一样。

如何获取它以便通知管理器在屏幕关闭时真正提醒用户?

更新:如果我将计时器设置为 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);
            }
        }
       }

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);
            }
        }
       }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

相思故 2024-10-09 08:33:42

现在所有这些都工作正常,但如果屏幕碰巧关闭,计时器会继续运行,并且通知会在正确的时间可用,但在我重新打开屏幕之前不会真正振动或响起。然后它会显示通知,就像它在队列中等待一样。

如何获取它以便通知管理器在屏幕关闭时真正提醒用户?

当屏幕关闭时,CPU 将很快停止运行,除非有东西持有 WakeLock

这意味着以下两件事之一:

  1. 您了解所有这些并持有 WakeLock。从用户喜欢他们的设备的角度来看,这可能是也可能不是一个好主意(例如,良好的电池寿命)。尽管如此,您可能需要保持更强的WakeLock,以使屏幕至少保持暗淡。我还没有尝试在 WakeLock 下发出 Notification,因此我不确定所有规则是什么。

  2. 您不了解所有这些,因此认为您的计时器正在运行,而实际上,设备已进入睡眠状态且 CPU 已关闭。当CPU再次打开时,你的定时器将立即关闭。

使用 AlarmManager 允许您执行基于计时器的事件来唤醒设备,并且不需要您的代码同时挂在内存中。我不知道你想在这里做什么(从你的描述来看似乎很奇怪),但是 AlarmManager 可能是值得研究的东西来替代你的计时器。

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:

  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.

  2. 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.

殊姿 2024-10-09 08:33:42

当手机被其他应用程序唤醒时,它可能会关闭,例如电子邮件应用程序定期检查新电子邮件。您需要设置自己的闹钟,以便在您需要的时间触发。 http://developer.android.com/reference/android/app/AlarmManager.html

NotificationManager 没有时间概念。它必须以某种方式被触发。您的情况的问题是,如果您让手机休眠,它就会这样做。警报是专门存在的,因此您可以在需要时唤醒设备。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文