Sunday, July 31, 2011

[android-developers] How to make an alarm live even when the application which set it is killed?


I have a little confusion with setting an alarm. If anyone could give
feedback or some hints it'll be greatly appreciated. In summary here
is my situation:

Inside my application I set an alarm:

// Set an alarm

// Actually I set a unique id here. The hardcoded value is for
simplicity only.
int id = 123454321;

Intent intent = new Intent(context, MyAlarm.class);
PendingIntent sender = PendingIntent.getBroadcast(context, id, intent,

long firstTime = SystemClock.elapsedRealtime();
firstTime += 10*1000;

AlarmManager am =
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime,
15*1000, sender);


// MyAlarm implementation

public class MyAlarm extends BroadcastReceiver
public void onReceive(Context context, Intent intent)
// Do some periodic work here


When I set the alarm everything is fine and good. I can see my alarm
when I use the command:

adb shell dumpsys alarm

I can see the alarm even when my application goes to the background.

Next I manually kill my application through

Settings - Applications - Manage applications - <MyApp> - Force stop

When I re-issue the command "adb shell dumpsys alarm" I see that my
alarm is gone. This makes me think that when my application goes into
the background and the framework kills it to free resources, my alarm
will also be killed. However, I would like to have an alarm which will
live independent of whether the application is running or not. Does
anybody have hints how to do this?

Thank you in advance guys,

You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to
To unsubscribe from this group, send email to
For more options, visit this group at

No comments:

Post a Comment