What is the right way to get updates from REST service by timer in Android? [closed]
I wonder what is the right way to get updates from REST service by timer in Android?
May be I should use timer task for this?
UPDATE: I need to receive updates even if user has switched to another application or receive a call. UI also should be updated. So when user switch back to the application he should see updated view.
Here is an example that might help:
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask(){ public void run()
{onTimerTick();}}, 0, 1000L);
private onTimerTick_()
{
// do REST call
}
You will want to make sure this is in a seperate thread.
'The right' way depends on what you are trying to do. Update your GUI? Update a datastore in the background while the app is not visible? AlarmManager lets you fire off a service to get data even if your app is not currently running, unlike TimerTask, so it's better for the second case.
精彩评论