Sunday, July 31, 2011

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

Hi,

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,
0);

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

AlarmManager am =
(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime,
15*1000, sender);

////////////////////////


////////////////////////
// MyAlarm implementation

public class MyAlarm extends BroadcastReceiver
{
@Override
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,
-Ali

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment