ik024
ik024

Reputation: 3596

Update a textview even while user is not using my app

I basically want to create a timer. I have a textview where I need to show a timer like "Updating your location in 2min 29 secs" and I want the timer to decrement For eg 2min 28secs followed by 2min 27 secs.

And I want to update it even when the user is not using my app (I don't want it to update in real sense what I mean is when user opens my app after say 1 min then the timer should show 1min 27 secs).

Can some one please help me out in pointing out the correct and efficient way of doing this ? Thanks :)

Upvotes: 4

Views: 74

Answers (2)

mmlooloo
mmlooloo

Reputation: 18977

The most efficient way in my opinion is using scheduleAtFixedRate when your activity is in foreground and when it is going to background you do not need any services you can just save the current time and the remaining time in a sharedpreferances and when again your activity comes to foreground read those values plus current time and then update the textview in a proper way.

Upvotes: 2

Junior Buckeridge
Junior Buckeridge

Reputation: 2085

You could use a service and send intents that your activity receives (the one with the TextView) using LocalBroadcastManager. You should register a BroadcastReceiver inside Activity.onResume(), and unregister it inside onPause().

Using a TimerTask should be the default approach but you must be aware that if your activity is sent to background it can be killed by the system. That's why I recommend using a service.

Hope it helps.

Upvotes: 1

Related Questions