Take the 2-minute tour ×

My application shows content for a site that also has a notification system. I want to show if there are new notifications, and I am using an AlarmManager that calls an IntentService.

My question is: where should I start/register this AlarmManager? I've put it in the onCreate() of my activity just for proof-of-concept (and its working fine, thank you very much :) ), but if you would start that activity twice, you would get multiple alarms.

The only possible solution I've come up with is this, but I don't know if this would be best practice

  • Start the manager in an onCreate() if the preference "alarm started" is false
  • Set some variable that it is started in preferences.

Now if the alarm stops for some reason, there's no way to restart it. So, a variation would be:

  • Always call cancel in the onCreate()
  • And then always set the Alarm.

This seems like a common pattern: Wanting to periodically get information with an alarm, and not setting that alarm more then once. How should I do this? When, where and how to register the alarm?

Also, continueing on @Zelimir 's comment: can you check if a certain alarm is allready set?

Ideally, the alarm would be set regardless of the activity being started or not of course, but that might be another thing.

For completeness, this is the code I'm currently using to start the alarm:

AlarmManager alMan   = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent i             = new Intent(this, CommentService.class);
PendingIntent penInt = PendingIntent.getService(this, 0, i, 0);


For even more completeness, the app description / situation.
The app is basically showing blogs (journals if you will) from a certain page. It has activities for adding entry, viewing entries, adding comments, etc. On the 'mother' site there is an option to recieve notifications (like the number you see here on SO too when you get a message). I want to show if there are new messages, and so retrieve them every xx minutes. It would be shown in the notificationbar for now, but it might feed some sort of widget later.

If you need more info: the app is called Androblip and it supports a site called blipfoto.com

share|improve this question
For similar problem I used the Service and from activity (or several of them) communicate using startService() and Intents. If service is not started it will be started and Intent processed, if yes, it will just switch to process the Intent. I would then suggest to start AlarmManager in onCreate() of the Service. Of course, you need clear rule when to stopService(). –  Zelimir Mar 16 '11 at 10:52
But then i'd have an extra service running, that will clear the alarm when it stops. So i'd have an alarm that calls a service, but also an ever-present service. That feels like double work. The advantage would be that you can actually check if a service is running, and not if an alarm is set? –  Nanne Mar 16 '11 at 10:56
You are right, it is double work for some SystemService like AlarmService and does not make much sense (although it may be useful to keep boolean saying if AlarmService was started in the past). I have found it useful for the case when I had BroadcastReceiver and screen rotation (Activity re-created). In that case I had actual state available independent of what happens to the Activity. If BroadcastReceiver for the system broadcasts was at the Activity, you need to register/unregister it all the time. Better is to register BroadcastReceiver at the Service. –  Zelimir Mar 16 '11 at 11:39

1 Answer 1

up vote 4 down vote accepted

When, where and how to register the alarm?

That is impossible to answer in the abstract. It depends entirely upon what the business rules are for your app, which you declined to supply in your question.

If the monitoring is to be happening all the time, a typical pattern is to register the alarm:

  • in onCreate() of your main activity for the very first run of your app
  • in a BOOT_COMPLETED BroadcastReceiver, to handle reboots, which wipe the AlarmManager roster

can you check if a certain alarm is allready set?

No, but you can cancel it without issue. Just create an equivalent PendingIntent and call cancel() on the AlarmManager.

share|improve this answer
But to avoid setting multiple alarms I need to delete them first. In the boot_completed this would not be a problem ofcourse, but as I need to only set the alarm once in the onCreate, it feels a bit of extra work to delete/set the alarm every time. It could also mean skipping some alarms if you delete / set it just before one would go off, couldn't it? I would gladly provide more logic, but didn't think it needed even more text :). –  Nanne Mar 16 '11 at 12:17
added description of the app for you :) –  Nanne Mar 16 '11 at 12:21
@Nanne: "But to avoid setting multiple alarms I need to delete them first." -- not in this case. "but as I need to only set the alarm once in the onCreate, it feels a bit of extra work to delete/set the alarm every time" -- per my advice above, only set the alarm once, on the first run of your app. –  CommonsWare Mar 16 '11 at 12:22
I'm not sure what more you want for "business rules". What do you need to see? I thought the usecase of getting a "unread" count from a site was pretty standard, so I didn't want to add even more text to the allready long post... –  Nanne Mar 16 '11 at 12:24
@Nanne: Your edit and my comment collided in space-time. :-) Please re-read my previous comment, since edited. –  CommonsWare Mar 16 '11 at 12:29

Your Answer


