Jaswanth Kumar
Jaswanth Kumar

Reputation: 3611

How to pause and resume Chronometer in Notification Panel?

I have a chronometer in notification panel which I instantiate using

remoteView.setChronometer(R.id.notification_timer, SystemClock.elapsedRealtime(),
                          null, true);

How should I pause the timer and resume it when I call Pause/Play button on Notification Panel?

Thanks in advance!

Upvotes: 0

Views: 4026

Answers (1)

sam
sam

Reputation: 2820

Try this code when you pause:

long timeDifference = 0;
Chronometer chronometer = (Chronometer) findViewById(R.id.notification_timer);
timeDifference  = chronometer.getBase() - SystemClock.elapsedRealtime();
remoteView.setChronometer(R.id.notification_timer, SystemClock.elapsedRealtime(),
                          null, false);

When you resume your timer,

remoteView.setChronometer(R.id.notification_timer,
                          timeDifference + SystemClock.elapsedRealtime(), null, true);

Hope this helps.

Upvotes: 2

Related Questions