Android notification manager does not work with the screen off

I have a countdown timer, when it turns off (to zero), it checks if the application has focus. If it does not start a notification in the notification panel. When you click on the notification, the application opens again. Now all this works fine, but if the screen turns off, the timer continues to work, and the notification is available at the right time, but never vibrates and does not hesitate until I return the screen. Then it displays a notification like waiting in line or something like that.

How do I get it so that the notification manager actually alerts the user when the screen is off?

Update: if I set the timer to 2 minutes, it will take another 2-3 minutes to actually execute the notification. So it works, but it is on a huge delay!

Code: therefore, I configure the notification service when the application loses focus, and when MyCount1 is finished, checks if the application has focus, and if it does not show a notification. All this works when the screen backlight is on. As soon as he disconnects, he 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);
            }
        }
       }
+3
source share
2 answers

Now all this works fine, but if the screen turns off, the timer continues to work, and the notification is available at the right time, but never vibrates and does not hesitate until I return the screen. Then it displays a notification like waiting in line or something like that.

, , ?

, CPU , - WakeLock.

:

  • WakeLock. , , (, ). , WakeLock, . Notification, WakeLock, , .

  • , , , . CPU , .

AlarmManager , , , . , ( ), AlarmManager - .

+2

, , - , , . . http://developer.android.com/reference/android/app/AlarmManager.html

NotificationManager . - . , , . , , .

+2

Source: https://habr.com/ru/post/1774623/


All Articles