1

I want to make a time or date listener which for example invoke an activity every day at 9 AM. I am not sure what is the best way to do that? I know I can use Alarm Manager, but I don't know how to make it repetitive?

Does anyone know? Thank you very much in advance.

Cheer :)

Zardaloop
  • 1,594
  • 5
  • 22
  • 43

1 Answers1

4

I know I can use Alarm Manager, but I don't know how to make it repetitive?

Use setRepeating() and specify a repeat interval of INTERVAL_DAY:

    static void scheduleAlarms(Context ctxt) {
      AlarmManager mgr=(AlarmManager)ctxt.getSystemService(Context.ALARM_SERVICE);
      Intent i=new Intent(ctxt, ScheduledService.class);
      PendingIntent pi=PendingIntent.getService(ctxt, 0, i, 0);

      mgr.setRepeating(AlarmManager.ELAPSED_REALTIME,
                       SystemClock.elapsedRealtime() + AlarmManager.INTERVAL_DAY,
                       AlarmManager.INTERVAL_DAY, pi);
  }

The above code will set up an alarm that will go off 24 hours from right now and every 24 hours thereafter. To have it start at 9am, replace ELAPSED_REALTIME with RTC and replace SystemClock.elapsedRealtime() + AlarmManager.INTERVAL_DAY with 9am (today or tomorrow), such as via a Calendar object.

Aurelius
  • 689
  • 1
  • 6
  • 16
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I think he gave you all you need. – Ovidiu Latcu Jun 18 '12 at 15:14
  • So if I set something like this : AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE); am.setRepeating(type, setTime(everyDay9), INTERVAL_DAY, myActivity.class); then what will be the type? the rest all make sense:) Thanks in advance – Zardaloop Jun 18 '12 at 15:16
  • Wow Many thanks, you are star :) I will try it, I am sure it will work. Just the other question is, does this work if I simply apply this method in the main activity class or should I make broadcastservice receiver class which call this class that holds this method in it? Apologies if my questions sounds silly, I have just started learning android. – Zardaloop Jun 18 '12 at 15:40
  • @user1163454: Since the alarm schedule is wiped out on a reboot, a common pattern is to schedule the alarms when the app first runs, when the device reboots (via a `BOOT_COMPLETED` receiver), and if you determine that the alarm has not happened in too long (because the user used Force Stop to terminate your app forcibly). In the case of an every-9am alarm, you could combine the first and third by simply rescheduling the alarm on every run of your app, plus on reboot. – CommonsWare Jun 18 '12 at 15:45
  • Thank you so much.I know I am getting annoying here but how do you determine the alarm has not happend if the system is rebooted, is there anyway to keep track of it?should I use a database to keep track of all the success alert and their date maybe? – Zardaloop Jun 18 '12 at 15:53
  • @user1163454: A database, a simple file, or `SharedPreferences` would be reasonable choices. – CommonsWare Jun 18 '12 at 15:55
  • Hi mate, sorry if I am going back to the same question but which what should I replace the SystemClock.elapsedRealtime() + AlarmManager.INTERVAL_DAY?? I was thinking of something like that, but doesn't work : Calendar cal = Calendar.getInstance(); cal.set(Calendar.HOUR_OF_DAY, 4); cal.set(Calendar.MINUTE, 40); cal.set(Calendar.SECOND, 0); mgr.setRepeating(AlarmManager.RTC, cal.getTimeInMillis(), pi); is there anyway to set it for 9 am ? – Zardaloop Jun 19 '12 at 21:25
  • @user1163454: "is there anyway to set it for 9 am ?" -- use 9 instead of 4 in your HOUR_OF_DAY call, I imagine. – CommonsWare Jun 19 '12 at 22:05
  • LOOLL I know, I was actually trying to test it, silly me. Sure I have to set it to 9:)) hahaa, is there anyway to test it rather than waiting 24 hours?for example is there anyway to I schedule it for the next 5 minutes to see if it works? I am sorry if I am keep asking, the reason is, the documents of the android website for this class is really really limited and there are not much sources out there. – Zardaloop Jun 19 '12 at 22:20
  • @user1163454: "is there anyway to test it rather than waiting 24 hours?" -- change the clock in your emulator, via its Settings app. – CommonsWare Jun 19 '12 at 22:22