11

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);
Shakeeb Ayaz
  • 6,200
  • 6
  • 45
  • 64
  • u resolved your issue ? – Erum Feb 19 '15 at 12:12
  • @ErumHannan yes it worked..Actually I booting my device at some point of time so it was not working.So for proper working of app I reinitialized the alarm on bootcomplete broadcast. That's it. – Shakeeb Ayaz Feb 20 '15 at 06:40

4 Answers4

15

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).

Community
  • 1
  • 1
Pankaj Kumar
  • 81,967
  • 29
  • 167
  • 186
  • I am sure device is not rebooted ..What do you mean by `application has been killed ` can you elaborate a little – Shakeeb Ayaz Nov 07 '13 at 04:55
  • If you removed an application from recent list, or from Application Manager you kill the app. That means you are force fully killed the application. and Then AlarmManager of your application has been removed. So only only one way to get ride of this behavior is to "Reset alarm on application launch" – Pankaj Kumar Nov 07 '13 at 04:58
  • I don't understand. They say "automatically starting the target application if it is not already running". You say "it will work until your application has been killed". If the app is not running, then according to them the alarm is alive and the app will be started when alarm gets off. But according to you, the alarm is not alive so nothing will be done? – Ahmed M Dec 31 '16 at 09:52
  • 1
    @AhmedM "it will work until your application has been killed" it means when use force stop application. This does not mean that your app is into background. Please let me know if more confusion – Pankaj Kumar Jan 02 '17 at 07:43
  • Hmm, I am not sure where you see it stating that the AlarmManager will remove your alarms if the app is killed.. I am doing testing while killing the app and have no such issues. The documentation only states on phone reboot that the AlarmManager is cleared. – Aaron Smentkowski Mar 15 '17 at 00:16
  • @AaronSmentkowski To test that, just kill the application and do not lunch application till the time when you scheduled your alarm. And better if you go through the links which I linked to my answer.. – Pankaj Kumar Mar 15 '17 at 05:19
  • @PankajKumar I did that. The alarm works as expected. I set an alarm for 20 seconds later, then kill the app, then wait and the alarm works. I specify my alarm code specifically in my answer below. – Aaron Smentkowski Mar 15 '17 at 05:26
  • @AaronSmentkowski How you are killing the application? Are your doing force close? – Pankaj Kumar Mar 15 '17 at 05:49
  • @AaronSmentkowski And if you have a question regarding it, please ask a separate question regarding the same with the code which you are using... I am sure you are using something like solution of http://stackoverflow.com/questions/5916859/how-to-save-alarm-after-app-killing. In that case you are handling app kill state, and your alarms are not being killed. – Pankaj Kumar Mar 15 '17 at 05:51
  • Swiping from recents, yup. I still see nowhere in the documentation where it says killing the app will remove your alarm either.. It specifically states it will *automatically start the target application*. I assume that means regardless of it having been previously started or not. – Aaron Smentkowski Mar 15 '17 at 05:52
  • 1
    Not looking for another question, trying to give better advice for this one because I was mislead by this answer to think that my alarms would be removed when my app is killed which is not the case and from what I can tell there is no documentation to back that up either. Which is why I posted my answer here also to elaborate. – Aaron Smentkowski Mar 15 '17 at 05:53
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/138077/discussion-between-pankaj-kumar-and-aaron-smentkowski). – Pankaj Kumar Mar 15 '17 at 05:56
3

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

Hardik
  • 17,179
  • 2
  • 35
  • 40
3

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.

  • 1
    @tej shah Is there any setting for background services on Lenovo phones? I assume this case can come up a lot with phone manufacturers, but this is the recommended way to handle this my Android. So if there is an OEM setting or something they did to change the way this works, you would have to work with them on that. – Aaron Smentkowski Sep 06 '17 at 16:22
  • Man what is the best answer for this ? –  Oct 19 '17 at 20:20
2

We need to enable our app in autostart manager in app manager, some handsets like Vivo v5,

In Vivo v5, we can find out this menu in

iManager > App Manager > Auto Start Manager > Enable our app here. 

Then your alarm / alarm manager will trigger alarm if the app is killed or closed.

Peyman Mohamadpour
  • 17,954
  • 24
  • 89
  • 100
Aneesh NN
  • 113
  • 1
  • 9