I think there are several ways to do this. I would not use a timer anyway.
You can use a handler to run your task when calling postDelayed. Then your task was to re-register using the handler from the inside.
final int ONE_SECOND = 1000; // one second final Handler handler = new Handler(); handler.postDelayed(new Runnable() { public void run() { ... // do some stuff if (expression) { handler.postDelayed(this, ONE_SECOND); } } }, ONE_SECOND);
This will cause the task to run during your application. You can also configure delayed speed in postDelayed in Runnable. This path is semi-predictable while you make another Looper. Using the main thread may or may not be appropriate depending on the task.
There is also an AlarmManager, which you can access through the Context interface, which is designed to perform repetitive tasks at more precise intervals. It's a little harder to use, but you get the flexibility to use RTC and keep repetitive tasks.
AlarmManager manager = mContext.getSystemService(Context.ALARM_SERVICE); manager.setRepeating(AlarmManager.RTC, <start_time_millis>, <period_millis>, pendingIntent);
For example, a pending intent can trigger a broadcast intent that you can listen to elsewhere. You can create this pendingintent in onCreate of your custom application object and cancel the intent in onTerminate ().
Greg giacovelli
source share