I'm trying to implement a foreground service and I'm trying to understand how it handles crashes.
I created a simple service like the one one below:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
checkRunningAndStartForeground();
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onCreate() {
super.onCreate();
crashAfterTenSeconds();
}
@Override
public void onDestroy {
super.onDestroy();
if(isRunning) {
isRunning = false;
stopForeground(true);
}
}
What happens is that the service starts in foreground, and then crashes, as expected. It is the scheduled for restart by the system. It restarts and then crashes again, as expected. After that, it is not scheduled for restart.
My question is, why does it restart once, but not twice? I want it to keep restarting after each crash.
What I am worried about is that if my application is running, and somehow crashes twice, then it will never be restarted.
For example, it crashes once in the beginning, runs for a day or two and crashes again, does that mean it won't come back?
As David suggested in the comments, I tried increasing the crash interval, but it did not help. I believe android stores the number of times the service crashes, and if it crashes 2 times, no matter the interval, then it stops restarting it. I believe this is different then when android kills off the service for memory, which I haven't gotten android to do for a foreground service.