Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will AlarmManager work if my application is not running?

I have an alarm that works fine if i am interacting(using) with my application but it dose not works if I set it for next day and not interacting with my app.Therefore I am getting doubt is this because my application process is not running at that time.

here is what I am doing

Calendar calSet = Calendar.getInstance();
calSet.set(Calendar.HOUR_OF_DAY, selectedhour);
calSet.set(Calendar.MINUTE, selectedminute);
calSet.set(Calendar.YEAR, year);
calSet.set(Calendar.MONTH, monthOfYear);
calSet.set(Calendar.DATE, dayOfMonth);
alarm = new Intent(ActivityA.this, Service.class);
pendingIntent = PendingIntent.getService(getApplicationContext(), i++,alarm, 1);
alarmanager.set(AlarmManager.RTC_WAKEUP, calSet.getTimeInMillis(),pendingIntent);
like image 223
Shakeeb Ayaz Avatar asked Nov 07 '13 04:11

Shakeeb Ayaz


3 Answers

From AlarmManager

AlarmManager provides access to the system alarm services. These allow you to schedule your application to be run at some point in the future. When an alarm goes off, the Intent that had been registered for it is broadcast by the system, automatically starting the target application if it is not already running. Registered alarms are retained while the device is asleep (and can optionally wake the device up if they go off during that time), but will be cleared if it is turned off and rebooted.


In simple way, it will work until your device has been rebooted.

You can read Android AlarmManager after reboot where @CommonsWare has been given a link of his sample application which persists Alarm even after device reboot.


Please ignore below section, it seems not valid. I will remove in future

You can read more about application kill at How to create a persistent AlarmManager, and How to save Alarm after app killing? can give you the idea about how to handle such issue (to persist alarm if application has been killed).

like image 145
Pankaj Kumar Avatar answered Oct 13 '22 10:10

Pankaj Kumar


Yes it worked but proper understanding see doc.

Note: The Alarm Manager is intended for cases where you want to have your application code run at a specific time, even if your application is not currently running. For normal timing operations (ticks, timeouts, etc) it is easier and much more efficient to use Handler.

see here http://developer.android.com/reference/android/app/AlarmManager.html

like image 22
Hardik Avatar answered Oct 13 '22 09:10

Hardik


Looking at the AlarmManager documentation..

http://developer.android.com/reference/android/app/AlarmManager.html

I don't see anywhere where it states that killing your app will remove all alarms that have been scheduled by that app. More specifically it states if your app is not started, it will start it for you.

I have done my own testing and can validate this by..

  • Setting an alarm 5 sec in the future.
  • Then closing app from recents.
  • Then watching logs for my broadcast to be received.
  • Keeping in mind this was done with a signed apk.

    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.MILLISECOND, 5000);
    
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
    

I would also keep in mind what Pankaj Kumar said about the restarting alarms on boot. That is the one place you need to cover yourself, because AlarmManager does clear all alarms on device restart.

like image 45
Aaron Smentkowski Avatar answered Oct 13 '22 09:10

Aaron Smentkowski