I'm using AlarmManager to have a background HTTP request occur every 2 minutes, which can result in a notification to the user depending on the response. This code runs before each request, scheduling the subsequent request:
AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, MyReceiver.class);
manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP
, SystemClock.elapsedRealtime() + CHECK_INTERVAL
, PendingIntent.getBroadcast(context, 0, intent, 0));
Sometimes an exception occurs, but according to the logs, it was after the call to manager.set
was completed successfully. Unfortunately, I'm finding the alarm does not fire after unexpected stop. Correction: didn't fire, perhaps because it took more than 2 minutes for the user to acknowledge the 'unexpected popup' dialog.
Is there a way to make my 'scheduling' to be crash-resistant? The users do not always restart this particular app after seeing the unexpected stop message.
Since the app is not available on Google Play Store for most people, I'm considering implementing an error-reporting and auto-restart via Thread.setDefaultUncaughtExceptionHandler()
, but I'm not ready to develop that far at this time; because then we'd lose the possibility of native error reporting for those who have it through Google Play. Is there a better approach to get the HTTP requests to continue after a crash, or is this it?
I know that fixing the cause of the crash would also take care of it, but I'm not perfect, so we should have a fallback plan.