I would like to run a function periodically every 5 seconds in a foreground service. I use android.os.Handler.postDelayed for that, like this:
private Handler handler = new Handler();
private Runnable updater = new Runnable() {
@Override
public void run() {
// Do some work
scheduleTask();
}
};
private void scheduleTask() {
handler.postDelayed(updater, 5000);
}
@Override
public void onCreate() {
super.onCreate();
scheduleTask();
}
@Override
public void onDestroy() {
super.onDestroy();
handler.removeCallbacks(updater);
}
Is it possible for the handler.postDelayed to fail? Can the OS just cancel the delayed request? How to catch that?
I need this to run stable for days.