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.
Quit functionality stops the service, releases the wake lock, and lets Android kill the process
If you are implying that you have a continuous WakeLock
, unless this is only running on devices that are perpetually plugged in, your users will slap you with a trout.
Also, Android can kill the process whenever it wants. Do not mistake startForeground()
as some sort of guarantee that Android will never kill your process.
But when it is killed from Task Manager is looks almost like alarm is getting cancelled.
I have no idea what "Task Manager" is, since there is none in Android. If you mean the list of running apps in Settings, and clicking Force Stop, then yes, that cancels any outstanding alarms and prevents your app from ever running again until something manually starts one of your components (e.g., user launches your app from the launcher).
Am I doing something wrong in regards to how to properly set the alarms?
Probably not. Force-stop definitely wipes out your alarms.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With