Q:

counting time while activity is not visible

My app needs to count time from the moment the user clicks a button and until he clicks it again. The user should be able to "walk around" other apps and activities and when he opens my app again - the counter will keep counting steady.
Right now I have the counter running in the background with an AsyncTask but when the user "Walks around" and fiddle with other apps and activities and then come back - my app is not counting. It's like you've started the app for the first time.
What do I need to do in order to make it keep working and counting while in the background and through the activity lifecycle?
thanks

A:

Why not record the time your count begins and subtract it from the time your app reappears and then use that value resume whatever dynamic counting task your display has to accomplish? 
ex:
1. user starts app at 000 secs. sys timestamp in millisecs is 11100000 (or whatever)
2. app runs for 5 seconds.
3. user opens their email app, etc.
4. after 20 seconds the user resumes the timer app.
5. app calculates offset; 11100025 -11100000 = 25.
6. app resumes "tick" at 25.

