Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android alarm is cancelled after closing the application

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!

like image 334
Allan Denot Avatar asked Jan 10 '11 05:01

Allan Denot


People also ask

How do I fix my alarm not working on my Android?

Step 1: Launch the Settings and click on Apps and Notifications. Step 2: Now, click on the Clock app and then further tap on Storage. Step 3: Finally, tap on Clear Cache and Clear Storage, one by one. A simple restart will conclude the steps and resolve the Android alarm no sound issue.

How do I make sure my Android alarm is off?

Open your phone's Clock app . At the bottom, tap Alarm. On the alarm you want, tap the On/Off switch.

How do I turn off alarm manager on Android?

In order to delete : AlarmManager alarmManager = (AlarmManager) getSystemService(Context. ALARM_SERVICE); Intent myIntent = new Intent(getApplicationContext(), SessionReceiver.

Does alarm Manager persist even after reboot?

The AlarmManager service is a convenient class to schedule working on your Android application; however, when the device shuts down or reboots, all your alarms will be lost since the system does not retain them between system restarts.


1 Answers

This is normal behaviour. If the user voluntarily force stop the applicaiton, then it should be stopped. Else, you are creating a virus like application.

If you really want, you could write another service which monitors if your other service is running and runs the service if the one is not running. But this will be another application and (you hope) the user wont kill this app using task manager.

Personally, I wouldn't worry. If the user stopped it, they wanted to stop it. Don't annoy the user.

like image 97
GSree Avatar answered Sep 18 '22 06:09

GSree