I have an Android application that has a service that runs in the background, communicates with a remote server and displays notifications to users if needed. The app also has an activity for users to interact with.
One of the requirements is that the application needs to start itself automatically in the morning. At the end of the day the user is free to quit the app (Quit functionality stops the service, releases the wake lock, and lets Android kill the process). In order to automatically restart the application I am scheduling an alarm using AlarmManager and PendingIntent:
//create intent to alert OnStartReceiver when alarm goes off
Intent i = new Intent(context, OnStartReceiver.class);
i.setAction("com.pointtrace.intents.alarm");
i.addFlags(Intent.FLAG_DEBUG_LOG_RESOLUTION|Intent.FLAG_FROM_BACKGROUND);
//wrap inside PendingIntent so that it can be used from outside
//our application
PendingIntent pi = PendingIntent.getBroadcast(context, 1, i, PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarms = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Log.d("GXT", "Scheduling alarm action(i)=" + i.getAction());
//set alarm to go off at wake time
alarms.set(AlarmManager.RTC_WAKEUP, getWakeTime(), pi);
OnStartReceiver class is registered to receive broadcasts and will restart the app when alarm occurs.
Everything works fine unless the user kills the application from Task Manager. If application is exited normally by either being killed by Android OS or my me calling System.exit(0); it restarts fine. But when it is killed from Task Manager is looks almost like alarm is getting cancelled.
I could not find anything in the Android documentation that would imply that alarms would be cancelled by Task Manager. Am I doing something wrong in regards to how to properly set the alarms?
I've tried removing the flags and just using 0 but it did not make any difference.
Thanks.