20

I am trying to use AlarmManager.AlarmClockInfo to set an alarm.

The constructor to this takes the time and a PendingIntent which is described in the docs as:

an intent that can be used to show or edit details of the alarm clock.

and then setAlarmClock( ) also takes in a pending intent which is described in the docs as:

Action to perform when the alarm goes off

I understand the use of the PendingIntent by setAlarmClock( ), however, how is the PendingIntent used by AlarmClockInfo and how do I use it to edit the details of the alarm clock?

An SO User
  • 24,612
  • 35
  • 133
  • 221

1 Answers1

32

however, how is the PendingIntent used by AlarmClockInfo and how do I use it to edit the details of the alarm clock?

Quoting myself from this book:

The biggest issue with setAlarmClock() is that it is visible to the user:

  • The user will see the alarm clock icon in their status bar, as if they had set an alarm with their device's built-in alarm clock app

  • The user will see the time of the alarm when they fully slide open their notification shade

Notification Shade, Showing Upcoming Alarm

  • Tapping on the alarm time in the notification shade will invoke the PendingIntent that you put into the AlarmClockInfo object

So, given this code...:

  static void scheduleAlarms(Context ctxt) {
    AlarmManager mgr=
      (AlarmManager)ctxt.getSystemService(Context.ALARM_SERVICE);
    Intent i=new Intent(ctxt, PollReceiver.class);
    PendingIntent pi=PendingIntent.getBroadcast(ctxt, 0, i, 0);
    Intent i2=new Intent(ctxt, EventDemoActivity.class);
    PendingIntent pi2=PendingIntent.getActivity(ctxt, 0, i2, 0);

    AlarmManager.AlarmClockInfo ac=
      new AlarmManager.AlarmClockInfo(System.currentTimeMillis()+PERIOD,
        pi2);

    mgr.setAlarmClock(ac, pi);
  }

(from this sample project)

...when the user taps on the time in the notification shade, EventDemoActivity will appear. The idea is that you should supply an activity here that allows the user to cancel or reschedule this alarm.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 2
    *The idea is that you should supply an activity here that allows the user to cancel or reschedule this alarm* Can this not be done by using a `Notification` with two actions - one to dismiss and one to snooze? – An SO User Jan 09 '16 at 22:34
  • @LittleChild: Sure, if you want to give the user two ways of managing the alarm (`Notification` and the alarm-supplied activity), and two icons in the status bar (one for the `Notification` and one from the system for the alarm). – CommonsWare Jan 09 '16 at 22:39
  • @Ankhwatcher: That is an excellent question... and I have no idea. If nothing else, you can cancel the `PendingIntent`, but it would be cleaner to be able to cancel the whole `AlarmClockInfo`, and I do not see how to do that. You might try asking a separate Stack Overflow question on that. – CommonsWare Jul 04 '17 at 11:05
  • @CommonsWare oh I deleted the question because it was silly. You can delete alarms set this way just the same as normal you just need to call cancel on a recreated PendingIntent with the same content as "pi" in this example. I thought you had to do something else because i got my pending intents mixed up when I implemented it. – Ankhwatcher Jul 04 '17 at 12:40