Reputation: 812
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?
Upvotes: 1
Views: 880
Reputation: 3121
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
Upvotes: 1
Reputation: 3440
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.
Upvotes: 3