I have a problem with AlarmManager, I set the code for scheduling a repeating alarm and after I run the application, the alarm runs fine. Even if I click on Home button (and the application is paused), the alarm still runs on its interval.
The problem is if I open the Task Manager and force close the application, then the alarm stops from running.
Is this a normal behavior, is there any way to avoid this and keep the alarm running after closing the application?
The code is below - the method is called by the ApplicationContext class, onCreate().
private void scheduleAlarm() {
if (alarmScheduled == true) { return; } // we only need to schedule once.
int alarmInterval = Def.pref(getApplicationContext()).getInt("alarmInterval", 30);
final Intent intent = new Intent(getApplicationContext(), CollectorAlarmReceiver.class);
final PendingIntent pending = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, 0);
AlarmManager alarmMgr = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
alarmMgr.cancel(pending); // cancel others.
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+1000,
alarmInterval*1000, pending);
Def.log(TAG,"scheduleAlarm(): alarm scheduled, interval: "+alarmInterval+" seconds");
alarmScheduled = true;
}
Receiver code:
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "CollectorAlarmReceiver invoked, starting CollectorService in background");
context.startService(new Intent(context, CollectorService.class));
Intent collectorService = new Intent(context,CollectorService.class);
collectorService.putExtra("action", CollectorService.ACTION_BACKGROUND_REQUEST_MESSAGES);
context.sendBroadcast(collectorService);
}
Thanks!