Return to Video

Alarms

  • 0:09 - 0:14
    Hi, I'm Adam Porter, and this is
    Programming Mobile
  • 0:14 - 0:18
    Applications for Android Handheld Systems.
  • 0:19 - 0:25
    The example applications that we've
    studied up to now have made decisions
  • 0:25 - 0:29
    and then taken action, now.
    But what
  • 0:29 - 0:34
    if your application makes a decision, but
    wants to take an action an hour from
  • 0:34 - 0:38
    now, tomorrow at midnight, or every 15
    minutes from now on?
  • 0:39 - 0:45
    In that case, your application will need
    to create and set alarms.
  • 0:47 - 0:52
    In this lesson, I'll start by discussing
    what alarms are and how they're used.
  • 0:53 - 0:56
    Next, I'll discuss the Android
    AlarmManager and
  • 0:56 - 0:59
    the APIs it provides for setting and
  • 0:59 - 1:01
    canceling alarms.
  • 1:02 - 1:07
    After that, I'll discuss the various types
    of alarms that Android supports.
  • 1:07 - 1:12
    And finally, I'll present and discuss an
    example application that uses alarms.
  • 1:14 - 1:19
    Now in a nutshell, alarms are a mechanism
    for sending intents
  • 1:19 - 1:25
    at some point, or points, in the future.
    And this is useful
  • 1:25 - 1:29
    because it allows an application to cause
    some other code
  • 1:29 - 1:34
    to execute even when that application is
    no longer running.
  • 1:36 - 1:40
    Once alarms have been created and
    registered, they're retained
  • 1:40 - 1:44
    and monitored even if the device goes to
    sleep.
  • 1:44 - 1:47
    And as we'll talk about later, depending
    on how you configure
  • 1:47 - 1:51
    an alarm, if it goes off while the device
    is sleeping, the
  • 1:51 - 1:55
    device can be woken up to handle the
    alarm, or the
  • 1:55 - 1:58
    alarm can be retained until the next time
    the device wakes up.
  • 2:00 - 2:01
    And alarms will continue to
  • 2:01 - 2:04
    be active until the device shuts down.
  • 2:04 - 2:08
    On shutdown, all registered alarms will be
    canceled.
  • 2:09 - 2:11
    [Alarm Examples]
  • 2:11 - 2:14
    To give you some examples of alarms, the
    other day, I was digging
  • 2:14 - 2:16
    through some Android source code and
  • 2:16 - 2:20
    came across several applications that use
    alarms.
  • 2:20 - 2:23
    For instance, the MMS messaging
    application uses
  • 2:23 - 2:26
    alarms to start a service that can
  • 2:26 - 2:29
    find MMS messages that haven't been
    delivered
  • 2:29 - 2:32
    and can try again to deliver them.
  • 2:32 - 2:36
    The Settings application can made a device
    discoverable
  • 2:36 - 2:37
    over Bluetooth.
  • 2:37 - 2:43
    And when it does this, that application
    sets an alarm, and when the
  • 2:43 - 2:48
    alarm goes off, the application makes the
    device not discoverable anymore.
  • 2:49 - 2:53
    And the Phone application keeps a cache of
    user information.
  • 2:54 - 2:58
    This application uses alarms to
    periodically update that cache.
  • 3:00 - 3:01
    If you want to use alarms
  • 3:01 - 3:07
    in your own applications, you do so by
    interacting with the AlarmManager service.
  • 3:08 - 3:15
    To get a reference to this service, you
    call the context classes GetSystemService,
  • 3:15 - 3:21
    passing in the name of the service, in
    this case, Alarm_Service as a parameter.
  • 3:24 - 3:27
    Once you have a reference to the
    AlarmManager, you can
  • 3:27 - 3:32
    then use some of its methods to create and
    set alarms.
  • 3:32 - 3:37
    For instance, you can use the Set method
    to set a single alarm.
  • 3:37 - 3:43
    And this method has three parameters.
    A type, which we'll discuss shortly, a
  • 3:43 - 3:49
    long, representing the time at which the
    alarm should go off and a PeningIntent,
  • 3:49 - 3:52
    which encapsulates the operation that
    should occur
  • 3:52 - 3:56
    when the alarm finally does go off.
  • 3:56 - 3:59
    [Note: the following discussion applies to
    API Levels <= 18]
  • 3:59 - 4:03
    Beginning with API 19 (KitKat)
    Alarm delivery is inexact]
  • 4:03 - 4:09
    [See http://developer.android.com/reference/
    android/app/AlarmManager.html]
  • 4:09 - 4:13
    You can use this method, setRepeating, to
    set an alarm
  • 4:13 - 4:17
    that repeatedly goes off
    at specific intervals.
  • 4:18 - 4:23
    This method has four parameters:
    the three we saw in the Set method
  • 4:23 - 4:27
    and an additional long
    that specifies the amount of time
  • 4:27 - 4:32
    between each successive time
    that the alarm goes off.
  • 4:34 - 4:38
    Another AlarmManager method is
    setInexactRepeating.
  • 4:39 - 4:44
    This method is similar to setRepeating, in
    that the alarm should go off periodically,
  • 4:45 - 4:48
    but this method gives Android more
    flexibility
  • 4:48 - 4:52
    in deciding exactly when to fire the
    alarms.
  • 4:52 - 4:57
    For instance, Android might batch up
    multiple alarms of this kind
  • 4:58 - 5:00
    and fire them at the same time
  • 5:00 - 5:03
    so as not to wake up the
    device too many times.
  • 5:04 - 5:08
    And if you want to have this kind of
    behavior, then your time interval
  • 5:08 - 5:14
    must be one of the following constants,
    which specifies intervals of 15 minutes,
  • 5:14 - 5:20
    30 minutes, one hour, 12 hours,
    and 24 hours.
  • 5:21 - 5:24
    If you don't use one of these constants,
    then the behavior of the alarms
  • 5:24 - 5:30
    is the same thing that you would have gotten
    had you used setRepeating instead.
  • 5:32 - 5:37
    Now each of the three methods I just
    showed you took a parameter called Type.
  • 5:38 - 5:39
    Let's talk about alarm types now.
  • 5:41 - 5:46
    Android provides two degrees of
    configurability with respect to alarms.
  • 5:47 - 5:53
    One has to do with how time information is
    interpreted, and the other tells Android
  • 5:53 - 5:57
    how to respond if the device is sleeping
    when the alarm fires.
  • 5:58 - 6:00
    Let's look at each of these one at a time.
  • 6:02 - 6:07
    First, you remember that each of the alarm
    setting methods took a long
  • 6:07 - 6:12
    as a parameter, and I said that, that long
    represented a time.
  • 6:13 - 6:18
    Android alarms can interpret
    this long value in two different ways.
  • 6:19 - 6:23
    One: it can consider it to be a real
    or wall clock time,
  • 6:23 - 6:27
    and in this case, the long represents
    the number of milliseconds
  • 6:27 - 6:31
    since midnight January 1, 1970.
  • 6:32 - 6:38
    And two: it can interpret it to be
    the system's up time, and that is,
  • 6:38 - 6:43
    the amount of time since the time
    at which the system last booted up.
  • 6:44 - 6:48
    The second issue is what should
    Android do if the alarm goes --
  • 6:48 - 6:52
    if, when the alarm goes off, the
    device is sleeping.
  • 6:53 - 6:57
    One possibility is to wake up the device
    now, and deliver the intent.
  • 6:58 - 7:01
    Another choice is to let the device
    continue sleeping,
  • 7:02 - 7:05
    and to deliver the intent the next time
    the device wakes up.
  • 7:07 - 7:12
    So, putting all those together, there are
    four possibilities, defined as follows.
  • 7:12 - 7:15
    RTC_WAKEUP:
  • 7:16 - 7:19
    fire the alarm at the specified
    wall clock time.
  • 7:19 - 7:24
    If the device is asleep, wake it now
    and deliver the intent.
  • 7:26 - 7:27
    RTC:
  • 7:27 - 7:32
    Fire the alarm at the specified wall clock
    time, but if the device is asleep,
  • 7:32 - 7:33
    don't wake it up now.
  • 7:34 - 7:38
    Instead, deliver the intent when the
    device next wakes up.
  • 7:40 - 7:47
    And there is ELAPSEDREALTIME and
    ELAPSED
    REALTIME_WAKEUP. (add coment in Amara)
  • 7:47 - 7:51
    For these two types of alarms,
    Android fires the alarm
  • 7:51 - 7:55
    when the device has been up
    for the specified time,
  • 7:55 - 7:59
    and if the device is sleeping
    when the alarm goes off,
  • 7:59 - 8:03
    it will not be woken up with
    ELAPSED_REALTIME,
  • 8:03 - 8:09
    it will be woken up with
    ELAPSEDREALTIMEWAKEUP.
  • 8:10 - 8:12
    [PendingIntent]
  • 8:12 - 8:17
    The last part of the AlarmManager APIs
    that we'll discuss is the PendingIntent.
  • 8:18 - 8:24
    As we discussed back in user notifications,
    a PendingIntent holds a regular intent
  • 8:24 - 8:26
    and essentially serves
    as a permission slip,
  • 8:26 - 8:30
    in which one component allows
    a second component
  • 8:30 - 8:35
    to use the underlying intent
    as if it were the first component.
  • 8:36 - 8:40
    Three methods that can be used
    to create a PendingIntent are:
  • 8:40 - 8:46
    getActivity, which returns a PendingIntent
    that can be used to start an activity,
  • 8:47 - 8:53
    GetBroadcast, which returns a PendingIntent
    that can be used to broadcast an intent,
  • 8:54 - 8:57
    and getService,
    which returns a PendingIntent
  • 8:57 - 9:00
    that can be used to start a service.
  • 9:01 - 9:03
    So now that we've learned about alarms,
  • 9:03 - 9:07
    let's take a look at an example
    application called AlarmCreate.
  • 9:08 - 9:12
    This application uses alarms
    to gently encourage a student
  • 9:12 - 9:16
    to stop playing video games
    and return to his or her studies.
  • 9:17 - 9:18
    Let's take a look.
  • 9:23 - 9:24
    Here's my device.
  • 9:26 - 9:29
    I'll now start up the AlarmCreate
    application.
  • 9:31 - 9:36
    The application displays a simple user
    interface with four buttons.
  • 9:37 - 9:41
    One button, labeled Set Single Alarm, that
    sets
  • 9:41 - 9:44
    a single alarm to go off in two minutes.
  • 9:45 - 9:50
    One button, labeled Set Repeating Alarm,
    that sets a repeating alarm
  • 9:50 - 9:52
    that will go off in two minutes,
  • 9:52 - 9:56
    and then continue to go off
    every 15 minutes after that.
  • 9:57 - 10:03
    One button, labeled Set Inexact Repeating
    Alarm, that sets a repeating alarm
  • 10:04 - 10:08
    that should go off every 15 minutes,
    starting in about two minutes.
  • 10:09 - 10:14
    Now because this is an inexact alarm,
    Android will try to fire the alarm
  • 10:14 - 10:18
    every 15 minutes but will exercise
    a lot of flexibility
  • 10:18 - 10:21
    in when exactly those alarms go off.
  • 10:22 - 10:26
    And finally, one button labelled
    Cancel Repeating Alarm,
  • 10:27 - 10:32
    and that will cancel any currently active
    repeating or inexact repeating alarms. /////
  • 10:33 - 10:37
    Now, I'll click the button labeled Set
    Single Alarm,
  • 10:38 - 10:41
    and as you can see, the toast message
    suggests
  • 10:41 - 10:45
    that the application has now set a single
    alarm,
  • 10:45 - 10:48
    and in this case, the alarm should go off
    in
  • 10:48 - 10:48
    two minutes.
  • 10:49 - 10:51
    And when it does, the code will
  • 10:51 - 10:55
    place a user notification in the
    notification area.
  • 10:56 - 10:59
    Let's come back at that point and see what
    happens.
  • 11:01 - 11:07
    So, now we're back watching the device,
    and there's the notification
  • 11:07 - 11:14
    asking if I'm playing Angry Birds again.
    Let me pull down on the notification area.
  • 11:14 - 11:16
    The notification view tells me that this
    is
  • 11:16 - 11:19
    a kind reminder to get back to studying.
  • 11:20 - 11:23
    If I click on this notification view,
  • 11:23 - 11:26
    the AlarmCreate application gets brought
    back up.
  • 11:29 - 11:31
    Now before we move forward, let's take a
  • 11:31 - 11:34
    look at the source code for this
    application.
  • 11:37 - 11:40
    Now here I've opened the application in
    the
  • 11:40 - 11:42
    IDE, and now I'll open the main activity.
  • 11:44 - 11:46
    Let's take a look at the onCreate method.
  • 11:48 - 11:52
    First, the code gets a reference to the
    AlarmManager service.
  • 11:53 - 11:56
    Next, the code creates an intent whose
  • 11:56 - 11:59
    target is the alarm notification receiver
    class.
  • 12:01 - 12:02
    This intent is then wrapped in a
  • 12:02 - 12:07
    PendingIntent that will cause this intent
    to be broadcast.
  • 12:08 - 12:13
    And after that, the code creates a second
    intent, which this time is
  • 12:13 - 12:17
    targeted to another class called
    AlarmLoggerReceiver.
  • 12:17 - 12:22
    And as before, this intent is wrapped in a
    PendingIntent that
  • 12:22 - 12:25
    will ultimately cause the intent to be
    broadcast to that receiver.
  • 12:27 - 12:27
    Now,
  • 12:27 - 12:31
    scrolling down, the code sets up the four
    buttons that we saw earlier.
  • 12:33 - 12:39
    The first button, when pressed, will set
    up two one-shot alarms.
  • 12:39 - 12:44
    The first alarm is scheduled to go off two
    minutes after the button is pressed.
  • 12:45 - 12:49
    The second of this pair will go off about
    five seconds later.
  • 12:51 - 12:53
    The second button, when pressed,
  • 12:53 - 12:56
    will set up a pair of repeating alarms.
  • 12:56 - 13:01
    The first alarm is scheduled to go off in
    two minutes, or two minutes
  • 13:01 - 13:05
    after the button is pressed, and then go
    off every 15 minutes after that.
  • 13:07 - 13:12
    The second alarm of the pair will go off
    about five seconds after the first.
  • 13:14 - 13:20
    The third button, when pressed, will set
    up two inexact repeating alarms.
  • 13:20 - 13:23
    The first alarm is scheduled to go off
    roughly every
  • 13:23 - 13:28
    15 minutes, starting two minutes after the
    button is pressed.
  • 13:28 - 13:31
    And again, the second alarm is scheduled
    to
  • 13:31 - 13:34
    go off about five seconds after the first.
  • 13:34 - 13:40
    Now remember, because this is an inexact
    repeating alarm, Android has
  • 13:40 - 13:45
    considerable flexibility in the exact
    timing of both of those alarms.
  • 13:45 - 13:48
    And in particular, Android will try to
    minimize the
  • 13:48 - 13:51
    number of times it needs to wake up the
  • 13:51 - 13:55
    device if it's sleeping, for instance, by
    grouping separate
  • 13:55 - 13:57
    alarms to go off roughly at the same time.
  • 13:59 - 14:04
    Finally, the fourth button, when pressed,
    will cancel existing alarms.
  • 14:05 - 14:09
    In particular, this is important for
    repeating alarms, which will continue
  • 14:09 - 14:14
    to go off until they're cancelled or until
    the device shuts down.
  • 14:18 - 14:22
    Returning back to the running application,
    I'll now press
  • 14:22 - 14:26
    the Set Repeating Alarm button, and this
    sets some
  • 14:26 - 14:29
    new repeating alarms that will first go
    off in
  • 14:29 - 14:32
    two minutes and then every 15 minutes
    after that.
  • 14:32 - 14:36
    [BLANK_AUDIO]
  • 14:36 - 14:37
    Let's come back when we're ready.
  • 14:38 - 14:40
    Okay, so we're back now, and it's been
  • 14:40 - 14:43
    about two minutes since the alarms were
    set.
  • 14:44 - 14:47
    There's the notification showing that the
    alarm went off.
  • 14:49 - 14:52
    The next alarm will come along in about 15
    minutes.
  • 14:52 - 14:54
    Let's take a break now, and when we come
  • 14:54 - 14:58
    back, we'll examine the LogCat output for
    this application.
  • 15:01 - 15:04
    So a bit more than 15 minutes has
  • 15:04 - 15:07
    passed, and here's the application open in
    the IDE.
  • 15:07 - 15:14
    I'll now expand the LogCat view, and I'll
    filter the LogCat
  • 15:14 - 15:20
    output to just show log messages that have
    a tag coming from this application.
  • 15:20 - 15:26
    I'll type tag:alarm, and that leaves just
    the
  • 15:26 - 15:28
    messages that we're interested in now.
  • 15:30 - 15:35
    As you can see, there are four messages,
    two from the first time the alarms
  • 15:35 - 15:40
    fired, and two more from when the alarms
    went off again after 15 minutes.
  • 15:41 - 15:45
    And notice that the two alarms which were
    scheduled to go off with five
  • 15:45 - 15:52
    seconds between them, do in fact go off
    with that interval of time between them.
  • 15:52 - 15:54
    So those were repeating alarms.
  • 15:54 - 15:57
    Let's go back now to the application and
    take a look at
  • 15:57 - 15:59
    what happens when we use the
  • 15:59 - 16:03
    SetInexactRepeating method to set these
    same alarms.
  • 16:05 - 16:06
    So here's my device again.
  • 16:06 - 16:11
    And now I'll pull down on the notification
    area
  • 16:11 - 16:13
    and use it to go back to the application.
  • 16:14 - 16:18
    First, I'll cancel the existing alarms,
    and
  • 16:18 - 16:21
    next I'll set the inexact repeating
    alarms.
  • 16:23 - 16:26
    So let's let the application run for
    awhile, and then
  • 16:26 - 16:30
    we'll take another look at the LogCat
    output for this application.
  • 16:30 - 16:33
    So here we are back in the IDE, and
  • 16:33 - 16:35
    about 20 minutes has passed since we last
    talked.
  • 16:37 - 16:39
    Let's go back to the LogCat view.
  • 16:39 - 16:44
    And here you can see that there are now
    four new messages, two from the first time
  • 16:44 - 16:49
    the alarms fired and two more from the
    second time those alarms went off.
  • 16:52 - 16:58
    And some things to notice are that even
    though at 11:19, we set the alarms to go
  • 16:58 - 17:02
    off starting in two minutes, the first set
    of
  • 17:02 - 17:06
    alarms actually went off after four or
    five minutes.
  • 17:06 - 17:08
    And the second thing to notice, is that
    even
  • 17:08 - 17:10
    though the pair of alarms were scheduled
    to go
  • 17:10 - 17:15
    off with a five second delay between them,
    Android
  • 17:15 - 17:17
    has actually fired them at essentially the
    same time.
  • 17:17 - 17:23
    And again, because these are inexact
    repeating alarms,
  • 17:23 - 17:27
    Android was free to modify the exact alarm
    timings.
  • 17:31 - 17:34
    So that's all for our lesson on alarms.
  • 17:34 - 17:37
    Please join me next time for a discussion
    of networking.
  • 17:37 - 17:39
    See you then.
  • 17:39 - 17:44
    [BLANK_AUDIO]
Title:
Alarms
Description:

From "Programming Mobile applications for Android Handheld Systems"

more » « less
Video Language:
English
Claude Almansi edited English subtitles for Alarms
Claude Almansi edited English subtitles for Alarms
Claude Almansi edited English subtitles for Alarms
Claude Almansi edited English subtitles for Alarms
Claude Almansi edited English subtitles for Alarms
Claude Almansi edited English subtitles for Alarms
Claude Almansi commented on English subtitles for Alarms

English subtitles

Incomplete

Revisions