Android notification manager doesn't work with the screen is off

天涯浪子 提交于 2019-12-09 20:56:37

问题


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

回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/4169558/android-notification-manager-doesnt-work-with-the-screen-is-off

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!