androidandroid-notificationschronometer

Is it possible to stop the Notification Chronometer after it reaches a certain value (0:00)?


I'm using NotificationCompat.Builder with .setUsesChronometer(true).setWhen(Instant.now().toEpochMilli() + timeDifference.toMillis());.

The setWhen()-timestamp is in the future, so the Chronometer value got a - before the time and it counts down.

When it reaches the timestamp, it continues to change and the value is now positive (counts up).

Is it possible to deactivate the Chronometer at 0:00 or stop it before it starts to count up?

I've found setChronometerCountDown(true) but it's API24+ (I need 19), Android Studio says it cannot resolve this method and I think it just removes the minus sign when counting down so that does not help me.

If the answer is no, is there an alternative?

Updating the notification every second would affect the battery drain I guess?

I'm using RemoteViews in my Notification so the Chronometer of RemoteViews could be an alternative but I can't find a way to stop that one either.


Solution

  • I used a handler to stop the chronometer.

    mHandler = new Handler();
    mHandler.postDelayed(new Runnable() {
      @Override
      public void run() {
        mCollapsedView.setUsesChronometer(false);
        mNotificationManagerCompat.notify(1, mNotificationBuilder.build());
      }
    }, timeDifference.toMillis());
    

    It would still be helpful to get some feedback for my solution. I don't have much experience with Android.