문제

In an Activity, I need to call a web service every 30 seconds or so, and use the data to update the UI.

Is there a recommended approach on how to schedule a repetitive task?

Thanks

도움이 되었습니까?

해결책

If this is in a running activity, you could use Timer/TimerTask and a Handler, or you could use postDelayed() and an AsyncTask.

다른 팁

You will want to create a Handler. Here is a brief code example.

Make sure to acquire the WakeLock if you want this to be running after the screen is turned off (or more accurately, to prevent the CPU from shutting down). Use of AlarmManager is another alternative.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top