I have an android application in which I have to execute some tasks at very short intervals 20-30seconds. It is a requirement, it is somehow a “monitoring” app and it won’t be for commercial purposes so no need to worry about battery and data usage.
I am scheduling tasks using java.util.Timer like this :
mTimer.scheduleAtFixedRate(mUpdateDataTask,0,20000);
I know how to stop the tasks being executed using :
mTimer.cancel();
and I am doing this when my Service is stopped.
THE PROBLEM:
If my app crashes at some point, the onDestroy() method of the Service is not called and the mTimer never gets canceled, and those task keep getting executed. How can I be sure that they are canceled when my app crashes or it is force stopped from the Settings->Application ?
You need to investigate
UncaughtExceptionHandleras a way of catching the crash; you can cancel the timers in this handler.