1

I'm using Chronometer in android app for count up timer. Chronometer base is 0.

I want to keep a track of elapsed time since chronometer started, even after app is in BG, or killed.

I also, want to update timer on UI, when app is reopened. I went through examples using service. But not much clear.

Is there any way to do this without service?

pratiti-systematix
  • 792
  • 11
  • 28
  • You can use a file, preference or database. Set the start time and use it for calculate elapsed time when app is reopened or refreshed. – adalpari Jun 10 '16 at 11:04

2 Answers2

3

Yes, the simplest way would be to store the start time in SharedPreferences, plus a boolean if the timer is currently running or not.

System.currentTimeMillis() // Store this as start time in SharedPrefs

When the app gets destroyed, the SharedPreferences persist. When the user reopens the app, you can load the start time and the timer state (running or stopped) from the SharedPrefs and calculate the current time.

A. Steenbergen
  • 3,360
  • 4
  • 34
  • 51
1

You can use a file, preference or database. Set the start time and use it for calculate elapsed time when app is reopened or refreshed

adalpari
  • 3,052
  • 20
  • 39