1

I want to program an app that has two independent, repetitive alarms. There are two classes seem to be able do this: AlarmManager and AlarmClock. I've tested AlarmManager, but when Android restarts all alarms are cleared.

Which should I use?

Adrian Toman
  • 11,316
  • 5
  • 48
  • 62
Héctor Ortiz
  • 257
  • 1
  • 6
  • 17

2 Answers2

2

Use BroadcastReceiver to handle Android OS boot broadcast and reschedule your alarms.

  • Yeah, I've been testing.. And what do you think about alarmclock? – Héctor Ortiz Aug 23 '12 at 05:58
  • 1
    You don't need it. What I've suggested is a common pattern for apps which need alarms fired even after reboot. –  Aug 23 '12 at 07:16
  • Thanks but..If I schedule an alarm for example... for 5 minutes and I turn off the phone for 2 minutes, How I know the time that I should schedule in the class BroadcastReceiver for set the alarmmanager again?? – Héctor Ortiz Aug 24 '12 at 00:43
  • 1
    You can keep that time somewhere, maybe in SharedPreferences. Then when device is turned on, read that data and schedule alarms again. –  Aug 24 '12 at 06:57
1

AlarmManager services 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.

You may find the SO post helpful Android AlarmManager

AlarmManager mgr=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE); 
Intent i=new Intent(context, OnAlarmReceiver.class); 
PendingIntent pi=PendingIntent.getBroadcast(context, 0, i, 0);
mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), PERIOD, pi);

Whereas AlarmClock provider contains an Intent action and extras that can be used to start an Activity to set a new alarm in an alarm clock application.

Community
  • 1
  • 1
prayagupa
  • 30,204
  • 14
  • 155
  • 192