AlarmManager.java revision 4870e9d5eba59fb257a87f97f1adf0b734cf48d3
1/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.app;
18
19import android.annotation.SdkConstant;
20import android.annotation.SystemApi;
21import android.content.Context;
22import android.content.Intent;
23import android.os.Build;
24import android.os.Parcel;
25import android.os.Parcelable;
26import android.os.RemoteException;
27import android.os.UserHandle;
28import android.os.WorkSource;
29
30/**
31 * This class provides access to the system alarm services.  These allow you
32 * to schedule your application to be run at some point in the future.  When
33 * an alarm goes off, the {@link Intent} that had been registered for it
34 * is broadcast by the system, automatically starting the target application
35 * if it is not already running.  Registered alarms are retained while the
36 * device is asleep (and can optionally wake the device up if they go off
37 * during that time), but will be cleared if it is turned off and rebooted.
38 *
39 * <p>The Alarm Manager holds a CPU wake lock as long as the alarm receiver's
40 * onReceive() method is executing. This guarantees that the phone will not sleep
41 * until you have finished handling the broadcast. Once onReceive() returns, the
42 * Alarm Manager releases this wake lock. This means that the phone will in some
43 * cases sleep as soon as your onReceive() method completes.  If your alarm receiver
44 * called {@link android.content.Context#startService Context.startService()}, it
45 * is possible that the phone will sleep before the requested service is launched.
46 * To prevent this, your BroadcastReceiver and Service will need to implement a
47 * separate wake lock policy to ensure that the phone continues running until the
48 * service becomes available.
49 *
50 * <p><b>Note: The Alarm Manager is intended for cases where you want to have
51 * your application code run at a specific time, even if your application is
52 * not currently running.  For normal timing operations (ticks, timeouts,
53 * etc) it is easier and much more efficient to use
54 * {@link android.os.Handler}.</b>
55 *
56 * <p class="caution"><strong>Note:</strong> Beginning with API 19
57 * ({@link android.os.Build.VERSION_CODES#KITKAT}) alarm delivery is inexact:
58 * the OS will shift alarms in order to minimize wakeups and battery use.  There are
59 * new APIs to support applications which need strict delivery guarantees; see
60 * {@link #setWindow(int, long, long, PendingIntent)} and
61 * {@link #setExact(int, long, PendingIntent)}.  Applications whose {@code targetSdkVersion}
62 * is earlier than API 19 will continue to see the previous behavior in which all
63 * alarms are delivered exactly when requested.
64 *
65 * <p>You do not
66 * instantiate this class directly; instead, retrieve it through
67 * {@link android.content.Context#getSystemService
68 * Context.getSystemService(Context.ALARM_SERVICE)}.
69 */
70public class AlarmManager
71{
72    private static final String TAG = "AlarmManager";
73
74    /**
75     * Alarm time in {@link System#currentTimeMillis System.currentTimeMillis()}
76     * (wall clock time in UTC), which will wake up the device when
77     * it goes off.
78     */
79    public static final int RTC_WAKEUP = 0;
80    /**
81     * Alarm time in {@link System#currentTimeMillis System.currentTimeMillis()}
82     * (wall clock time in UTC).  This alarm does not wake the
83     * device up; if it goes off while the device is asleep, it will not be
84     * delivered until the next time the device wakes up.
85     */
86    public static final int RTC = 1;
87    /**
88     * Alarm time in {@link android.os.SystemClock#elapsedRealtime
89     * SystemClock.elapsedRealtime()} (time since boot, including sleep),
90     * which will wake up the device when it goes off.
91     */
92    public static final int ELAPSED_REALTIME_WAKEUP = 2;
93    /**
94     * Alarm time in {@link android.os.SystemClock#elapsedRealtime
95     * SystemClock.elapsedRealtime()} (time since boot, including sleep).
96     * This alarm does not wake the device up; if it goes off while the device
97     * is asleep, it will not be delivered until the next time the device
98     * wakes up.
99     */
100    public static final int ELAPSED_REALTIME = 3;
101
102    /**
103     * Broadcast Action: Sent after the value returned by
104     * {@link #getNextAlarmClock()} has changed.
105     *
106     * <p class="note">This is a protected intent that can only be sent by the system.
107     * It is only sent to registered receivers.</p>
108     */
109    @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
110    public static final String ACTION_NEXT_ALARM_CLOCK_CHANGED =
111            "android.app.action.NEXT_ALARM_CLOCK_CHANGED";
112
113    /** @hide */
114    public static final long WINDOW_EXACT = 0;
115    /** @hide */
116    public static final long WINDOW_HEURISTIC = -1;
117
118    /**
119     * Flag for alarms: this is to be a stand-alone alarm, that should not be batched with
120     * other alarms.
121     * @hide
122     */
123    public static final int FLAG_STANDALONE = 1<<0;
124
125    /**
126     * Flag for alarms: this alarm would like to wake the device even if it is idle.  This
127     * is, for example, an alarm for an alarm clock.
128     * @hide
129     */
130    public static final int FLAG_WAKE_FROM_IDLE = 1<<1;
131
132    /**
133     * Flag for alarms: this alarm would like to still execute even if the device is
134     * idle.  This won't bring the device out of idle, just allow this specific alarm to
135     * run.  Note that this means the actual time this alarm goes off can be inconsistent
136     * with the time of non-allow-while-idle alarms (it could go earlier than the time
137     * requested by another alarm).
138     *
139     * @hide
140     */
141    public static final int FLAG_ALLOW_WHILE_IDLE = 1<<2;
142
143    /**
144     * Flag for alarms: this alarm marks the point where we would like to come out of idle
145     * mode.  It may be moved by the alarm manager to match the first wake-from-idle alarm.
146     * Scheduling an alarm with this flag puts the alarm manager in to idle mode, where it
147     * avoids scheduling any further alarms until the marker alarm is executed.
148     * @hide
149     */
150    public static final int FLAG_IDLE_UNTIL = 1<<3;
151
152    private final IAlarmManager mService;
153    private final boolean mAlwaysExact;
154
155
156    /**
157     * package private on purpose
158     */
159    AlarmManager(IAlarmManager service, Context ctx) {
160        mService = service;
161
162        final int sdkVersion = ctx.getApplicationInfo().targetSdkVersion;
163        mAlwaysExact = (sdkVersion < Build.VERSION_CODES.KITKAT);
164    }
165
166    private long legacyExactLength() {
167        return (mAlwaysExact ? WINDOW_EXACT : WINDOW_HEURISTIC);
168    }
169
170    /**
171     * <p>Schedule an alarm.  <b>Note: for timing operations (ticks, timeouts,
172     * etc) it is easier and much more efficient to use {@link android.os.Handler}.</b>
173     * If there is already an alarm scheduled for the same IntentSender, that previous
174     * alarm will first be canceled.
175     *
176     * <p>If the stated trigger time is in the past, the alarm will be triggered
177     * immediately.  If there is already an alarm for this Intent
178     * scheduled (with the equality of two intents being defined by
179     * {@link Intent#filterEquals}), then it will be removed and replaced by
180     * this one.
181     *
182     * <p>
183     * The alarm is an Intent broadcast that goes to a broadcast receiver that
184     * you registered with {@link android.content.Context#registerReceiver}
185     * or through the &lt;receiver&gt; tag in an AndroidManifest.xml file.
186     *
187     * <p>
188     * Alarm intents are delivered with a data extra of type int called
189     * {@link Intent#EXTRA_ALARM_COUNT Intent.EXTRA_ALARM_COUNT} that indicates
190     * how many past alarm events have been accumulated into this intent
191     * broadcast.  Recurring alarms that have gone undelivered because the
192     * phone was asleep may have a count greater than one when delivered.
193     *
194     * <div class="note">
195     * <p>
196     * <b>Note:</b> Beginning in API 19, the trigger time passed to this method
197     * is treated as inexact: the alarm will not be delivered before this time, but
198     * may be deferred and delivered some time later.  The OS will use
199     * this policy in order to "batch" alarms together across the entire system,
200     * minimizing the number of times the device needs to "wake up" and minimizing
201     * battery use.  In general, alarms scheduled in the near future will not
202     * be deferred as long as alarms scheduled far in the future.
203     *
204     * <p>
205     * With the new batching policy, delivery ordering guarantees are not as
206     * strong as they were previously.  If the application sets multiple alarms,
207     * it is possible that these alarms' <em>actual</em> delivery ordering may not match
208     * the order of their <em>requested</em> delivery times.  If your application has
209     * strong ordering requirements there are other APIs that you can use to get
210     * the necessary behavior; see {@link #setWindow(int, long, long, PendingIntent)}
211     * and {@link #setExact(int, long, PendingIntent)}.
212     *
213     * <p>
214     * Applications whose {@code targetSdkVersion} is before API 19 will
215     * continue to get the previous alarm behavior: all of their scheduled alarms
216     * will be treated as exact.
217     * </div>
218     *
219     * @param type One of {@link #ELAPSED_REALTIME}, {@link #ELAPSED_REALTIME_WAKEUP},
220     *        {@link #RTC}, or {@link #RTC_WAKEUP}.
221     * @param triggerAtMillis time in milliseconds that the alarm should go
222     * off, using the appropriate clock (depending on the alarm type).
223     * @param operation Action to perform when the alarm goes off;
224     * typically comes from {@link PendingIntent#getBroadcast
225     * IntentSender.getBroadcast()}.
226     *
227     * @see android.os.Handler
228     * @see #setExact
229     * @see #setRepeating
230     * @see #setWindow
231     * @see #cancel
232     * @see android.content.Context#sendBroadcast
233     * @see android.content.Context#registerReceiver
234     * @see android.content.Intent#filterEquals
235     * @see #ELAPSED_REALTIME
236     * @see #ELAPSED_REALTIME_WAKEUP
237     * @see #RTC
238     * @see #RTC_WAKEUP
239     */
240    public void set(int type, long triggerAtMillis, PendingIntent operation) {
241        setImpl(type, triggerAtMillis, legacyExactLength(), 0, 0, operation, null, null);
242    }
243
244    /**
245     * Schedule a repeating alarm.  <b>Note: for timing operations (ticks,
246     * timeouts, etc) it is easier and much more efficient to use
247     * {@link android.os.Handler}.</b>  If there is already an alarm scheduled
248     * for the same IntentSender, it will first be canceled.
249     *
250     * <p>Like {@link #set}, except you can also supply a period at which
251     * the alarm will automatically repeat.  This alarm continues
252     * repeating until explicitly removed with {@link #cancel}.  If the stated
253     * trigger time is in the past, the alarm will be triggered immediately, with an
254     * alarm count depending on how far in the past the trigger time is relative
255     * to the repeat interval.
256     *
257     * <p>If an alarm is delayed (by system sleep, for example, for non
258     * _WAKEUP alarm types), a skipped repeat will be delivered as soon as
259     * possible.  After that, future alarms will be delivered according to the
260     * original schedule; they do not drift over time.  For example, if you have
261     * set a recurring alarm for the top of every hour but the phone was asleep
262     * from 7:45 until 8:45, an alarm will be sent as soon as the phone awakens,
263     * then the next alarm will be sent at 9:00.
264     *
265     * <p>If your application wants to allow the delivery times to drift in
266     * order to guarantee that at least a certain time interval always elapses
267     * between alarms, then the approach to take is to use one-time alarms,
268     * scheduling the next one yourself when handling each alarm delivery.
269     *
270     * <p class="note">
271     * <b>Note:</b> as of API 19, all repeating alarms are inexact.  If your
272     * application needs precise delivery times then it must use one-time
273     * exact alarms, rescheduling each time as described above. Legacy applications
274     * whose {@code targetSdkVersion} is earlier than API 19 will continue to have all
275     * of their alarms, including repeating alarms, treated as exact.
276     *
277     * @param type One of {@link #ELAPSED_REALTIME}, {@link #ELAPSED_REALTIME_WAKEUP},
278     *        {@link #RTC}, or {@link #RTC_WAKEUP}.
279     * @param triggerAtMillis time in milliseconds that the alarm should first
280     * go off, using the appropriate clock (depending on the alarm type).
281     * @param intervalMillis interval in milliseconds between subsequent repeats
282     * of the alarm.
283     * @param operation Action to perform when the alarm goes off;
284     * typically comes from {@link PendingIntent#getBroadcast
285     * IntentSender.getBroadcast()}.
286     *
287     * @see android.os.Handler
288     * @see #set
289     * @see #setExact
290     * @see #setWindow
291     * @see #cancel
292     * @see android.content.Context#sendBroadcast
293     * @see android.content.Context#registerReceiver
294     * @see android.content.Intent#filterEquals
295     * @see #ELAPSED_REALTIME
296     * @see #ELAPSED_REALTIME_WAKEUP
297     * @see #RTC
298     * @see #RTC_WAKEUP
299     */
300    public void setRepeating(int type, long triggerAtMillis,
301            long intervalMillis, PendingIntent operation) {
302        setImpl(type, triggerAtMillis, legacyExactLength(), intervalMillis, 0, operation, null,
303                null);
304    }
305
306    /**
307     * Schedule an alarm to be delivered within a given window of time.  This method
308     * is similar to {@link #set(int, long, PendingIntent)}, but allows the
309     * application to precisely control the degree to which its delivery might be
310     * adjusted by the OS. This method allows an application to take advantage of the
311     * battery optimizations that arise from delivery batching even when it has
312     * modest timeliness requirements for its alarms.
313     *
314     * <p>
315     * This method can also be used to achieve strict ordering guarantees among
316     * multiple alarms by ensuring that the windows requested for each alarm do
317     * not intersect.
318     *
319     * <p>
320     * When precise delivery is not required, applications should use the standard
321     * {@link #set(int, long, PendingIntent)} method.  This will give the OS the most
322     * flexibility to minimize wakeups and battery use.  For alarms that must be delivered
323     * at precisely-specified times with no acceptable variation, applications can use
324     * {@link #setExact(int, long, PendingIntent)}.
325     *
326     * @param type One of {@link #ELAPSED_REALTIME}, {@link #ELAPSED_REALTIME_WAKEUP},
327     *        {@link #RTC}, or {@link #RTC_WAKEUP}.
328     * @param windowStartMillis The earliest time, in milliseconds, that the alarm should
329     *        be delivered, expressed in the appropriate clock's units (depending on the alarm
330     *        type).
331     * @param windowLengthMillis The length of the requested delivery window,
332     *        in milliseconds.  The alarm will be delivered no later than this many
333     *        milliseconds after {@code windowStartMillis}.  Note that this parameter
334     *        is a <i>duration,</i> not the timestamp of the end of the window.
335     * @param operation Action to perform when the alarm goes off;
336     *        typically comes from {@link PendingIntent#getBroadcast
337     *        IntentSender.getBroadcast()}.
338     *
339     * @see #set
340     * @see #setExact
341     * @see #setRepeating
342     * @see #cancel
343     * @see android.content.Context#sendBroadcast
344     * @see android.content.Context#registerReceiver
345     * @see android.content.Intent#filterEquals
346     * @see #ELAPSED_REALTIME
347     * @see #ELAPSED_REALTIME_WAKEUP
348     * @see #RTC
349     * @see #RTC_WAKEUP
350     */
351    public void setWindow(int type, long windowStartMillis, long windowLengthMillis,
352            PendingIntent operation) {
353        setImpl(type, windowStartMillis, windowLengthMillis, 0, 0, operation, null, null);
354    }
355
356    /**
357     * Schedule an alarm to be delivered precisely at the stated time.
358     *
359     * <p>
360     * This method is like {@link #set(int, long, PendingIntent)}, but does not permit
361     * the OS to adjust the delivery time.  The alarm will be delivered as nearly as
362     * possible to the requested trigger time.
363     *
364     * <p>
365     * <b>Note:</b> only alarms for which there is a strong demand for exact-time
366     * delivery (such as an alarm clock ringing at the requested time) should be
367     * scheduled as exact.  Applications are strongly discouraged from using exact
368     * alarms unnecessarily as they reduce the OS's ability to minimize battery use.
369     *
370     * @param type One of {@link #ELAPSED_REALTIME}, {@link #ELAPSED_REALTIME_WAKEUP},
371     *        {@link #RTC}, or {@link #RTC_WAKEUP}.
372     * @param triggerAtMillis time in milliseconds that the alarm should go
373     *        off, using the appropriate clock (depending on the alarm type).
374     * @param operation Action to perform when the alarm goes off;
375     *        typically comes from {@link PendingIntent#getBroadcast
376     *        IntentSender.getBroadcast()}.
377     *
378     * @see #set
379     * @see #setRepeating
380     * @see #setWindow
381     * @see #cancel
382     * @see android.content.Context#sendBroadcast
383     * @see android.content.Context#registerReceiver
384     * @see android.content.Intent#filterEquals
385     * @see #ELAPSED_REALTIME
386     * @see #ELAPSED_REALTIME_WAKEUP
387     * @see #RTC
388     * @see #RTC_WAKEUP
389     */
390    public void setExact(int type, long triggerAtMillis, PendingIntent operation) {
391        setImpl(type, triggerAtMillis, WINDOW_EXACT, 0, 0, operation, null, null);
392    }
393
394    /**
395     * Schedule an idle-until alarm, which will keep the alarm manager idle until
396     * the given time.
397     * @hide
398     */
399    public void setIdleUntil(int type, long triggerAtMillis, PendingIntent operation) {
400        setImpl(type, triggerAtMillis, WINDOW_EXACT, 0, FLAG_IDLE_UNTIL, operation, null, null);
401    }
402
403    /**
404     * Schedule an alarm that represents an alarm clock.
405     *
406     * The system may choose to display information about this alarm to the user.
407     *
408     * <p>
409     * This method is like {@link #setExact(int, long, PendingIntent)}, but implies
410     * {@link #RTC_WAKEUP}.
411     *
412     * @param info
413     * @param operation Action to perform when the alarm goes off;
414     *        typically comes from {@link PendingIntent#getBroadcast
415     *        IntentSender.getBroadcast()}.
416     *
417     * @see #set
418     * @see #setRepeating
419     * @see #setWindow
420     * @see #setExact
421     * @see #cancel
422     * @see #getNextAlarmClock()
423     * @see android.content.Context#sendBroadcast
424     * @see android.content.Context#registerReceiver
425     * @see android.content.Intent#filterEquals
426     */
427    public void setAlarmClock(AlarmClockInfo info, PendingIntent operation) {
428        setImpl(RTC_WAKEUP, info.getTriggerTime(), WINDOW_EXACT, 0, 0, operation, null, info);
429    }
430
431    /** @hide */
432    @SystemApi
433    public void set(int type, long triggerAtMillis, long windowMillis, long intervalMillis,
434            PendingIntent operation, WorkSource workSource) {
435        setImpl(type, triggerAtMillis, windowMillis, intervalMillis, 0, operation, workSource,
436                null);
437    }
438
439    private void setImpl(int type, long triggerAtMillis, long windowMillis, long intervalMillis,
440            int flags, PendingIntent operation, WorkSource workSource, AlarmClockInfo alarmClock) {
441        if (triggerAtMillis < 0) {
442            /* NOTYET
443            if (mAlwaysExact) {
444                // Fatal error for KLP+ apps to use negative trigger times
445                throw new IllegalArgumentException("Invalid alarm trigger time "
446                        + triggerAtMillis);
447            }
448            */
449            triggerAtMillis = 0;
450        }
451
452        try {
453            mService.set(type, triggerAtMillis, windowMillis, intervalMillis, flags, operation,
454                    workSource, alarmClock);
455        } catch (RemoteException ex) {
456        }
457    }
458
459    /**
460     * Available inexact recurrence interval recognized by
461     * {@link #setInexactRepeating(int, long, long, PendingIntent)}
462     * when running on Android prior to API 19.
463     */
464    public static final long INTERVAL_FIFTEEN_MINUTES = 15 * 60 * 1000;
465
466    /**
467     * Available inexact recurrence interval recognized by
468     * {@link #setInexactRepeating(int, long, long, PendingIntent)}
469     * when running on Android prior to API 19.
470     */
471    public static final long INTERVAL_HALF_HOUR = 2*INTERVAL_FIFTEEN_MINUTES;
472
473    /**
474     * Available inexact recurrence interval recognized by
475     * {@link #setInexactRepeating(int, long, long, PendingIntent)}
476     * when running on Android prior to API 19.
477     */
478    public static final long INTERVAL_HOUR = 2*INTERVAL_HALF_HOUR;
479
480    /**
481     * Available inexact recurrence interval recognized by
482     * {@link #setInexactRepeating(int, long, long, PendingIntent)}
483     * when running on Android prior to API 19.
484     */
485    public static final long INTERVAL_HALF_DAY = 12*INTERVAL_HOUR;
486
487    /**
488     * Available inexact recurrence interval recognized by
489     * {@link #setInexactRepeating(int, long, long, PendingIntent)}
490     * when running on Android prior to API 19.
491     */
492    public static final long INTERVAL_DAY = 2*INTERVAL_HALF_DAY;
493
494    /**
495     * Schedule a repeating alarm that has inexact trigger time requirements;
496     * for example, an alarm that repeats every hour, but not necessarily at
497     * the top of every hour.  These alarms are more power-efficient than
498     * the strict recurrences traditionally supplied by {@link #setRepeating}, since the
499     * system can adjust alarms' delivery times to cause them to fire simultaneously,
500     * avoiding waking the device from sleep more than necessary.
501     *
502     * <p>Your alarm's first trigger will not be before the requested time,
503     * but it might not occur for almost a full interval after that time.  In
504     * addition, while the overall period of the repeating alarm will be as
505     * requested, the time between any two successive firings of the alarm
506     * may vary.  If your application demands very low jitter, use
507     * one-shot alarms with an appropriate window instead; see {@link
508     * #setWindow(int, long, long, PendingIntent)} and
509     * {@link #setExact(int, long, PendingIntent)}.
510     *
511     * <p class="note">
512     * As of API 19, all repeating alarms are inexact.  Because this method has
513     * been available since API 3, your application can safely call it and be
514     * assured that it will get similar behavior on both current and older versions
515     * of Android.
516     *
517     * @param type One of {@link #ELAPSED_REALTIME}, {@link #ELAPSED_REALTIME_WAKEUP},
518     *        {@link #RTC}, or {@link #RTC_WAKEUP}.
519     * @param triggerAtMillis time in milliseconds that the alarm should first
520     * go off, using the appropriate clock (depending on the alarm type).  This
521     * is inexact: the alarm will not fire before this time, but there may be a
522     * delay of almost an entire alarm interval before the first invocation of
523     * the alarm.
524     * @param intervalMillis interval in milliseconds between subsequent repeats
525     * of the alarm.  Prior to API 19, if this is one of INTERVAL_FIFTEEN_MINUTES,
526     * INTERVAL_HALF_HOUR, INTERVAL_HOUR, INTERVAL_HALF_DAY, or INTERVAL_DAY
527     * then the alarm will be phase-aligned with other alarms to reduce the
528     * number of wakeups.  Otherwise, the alarm will be set as though the
529     * application had called {@link #setRepeating}.  As of API 19, all repeating
530     * alarms will be inexact and subject to batching with other alarms regardless
531     * of their stated repeat interval.
532     * @param operation Action to perform when the alarm goes off;
533     * typically comes from {@link PendingIntent#getBroadcast
534     * IntentSender.getBroadcast()}.
535     *
536     * @see android.os.Handler
537     * @see #set
538     * @see #cancel
539     * @see android.content.Context#sendBroadcast
540     * @see android.content.Context#registerReceiver
541     * @see android.content.Intent#filterEquals
542     * @see #ELAPSED_REALTIME
543     * @see #ELAPSED_REALTIME_WAKEUP
544     * @see #RTC
545     * @see #RTC_WAKEUP
546     * @see #INTERVAL_FIFTEEN_MINUTES
547     * @see #INTERVAL_HALF_HOUR
548     * @see #INTERVAL_HOUR
549     * @see #INTERVAL_HALF_DAY
550     * @see #INTERVAL_DAY
551     */
552    public void setInexactRepeating(int type, long triggerAtMillis,
553            long intervalMillis, PendingIntent operation) {
554        setImpl(type, triggerAtMillis, WINDOW_HEURISTIC, intervalMillis, 0, operation, null, null);
555    }
556
557    /**
558     * Remove any alarms with a matching {@link Intent}.
559     * Any alarm, of any type, whose Intent matches this one (as defined by
560     * {@link Intent#filterEquals}), will be canceled.
561     *
562     * @param operation IntentSender which matches a previously added
563     * IntentSender.
564     *
565     * @see #set
566     */
567    public void cancel(PendingIntent operation) {
568        try {
569            mService.remove(operation);
570        } catch (RemoteException ex) {
571        }
572    }
573
574    /**
575     * Set the system wall clock time.
576     * Requires the permission android.permission.SET_TIME.
577     *
578     * @param millis time in milliseconds since the Epoch
579     */
580    public void setTime(long millis) {
581        try {
582            mService.setTime(millis);
583        } catch (RemoteException ex) {
584        }
585    }
586
587    /**
588     * Set the system default time zone.
589     * Requires the permission android.permission.SET_TIME_ZONE.
590     *
591     * @param timeZone in the format understood by {@link java.util.TimeZone}
592     */
593    public void setTimeZone(String timeZone) {
594        try {
595            mService.setTimeZone(timeZone);
596        } catch (RemoteException ex) {
597        }
598    }
599
600    /**
601     * Gets information about the next alarm clock currently scheduled.
602     *
603     * The alarm clocks considered are those scheduled by {@link #setAlarmClock}
604     * from any package of the calling user.
605     *
606     * @see #setAlarmClock
607     * @see AlarmClockInfo
608     */
609    public AlarmClockInfo getNextAlarmClock() {
610        return getNextAlarmClock(UserHandle.myUserId());
611    }
612
613    /**
614     * Gets information about the next alarm clock currently scheduled.
615     *
616     * The alarm clocks considered are those scheduled by {@link #setAlarmClock}
617     * from any package of the given {@parm userId}.
618     *
619     * @see #setAlarmClock
620     * @see AlarmClockInfo
621     *
622     * @hide
623     */
624    public AlarmClockInfo getNextAlarmClock(int userId) {
625        try {
626            return mService.getNextAlarmClock(userId);
627        } catch (RemoteException ex) {
628            return null;
629        }
630    }
631
632    /**
633     * An immutable description of an alarm clock.
634     *
635     * @see AlarmManager#setAlarmClock
636     * @see AlarmManager#getNextAlarmClock
637     */
638    public static final class AlarmClockInfo implements Parcelable {
639
640        private final long mTriggerTime;
641        private final PendingIntent mShowIntent;
642
643        /**
644         * Creates a new alarm clock description.
645         *
646         * @param triggerTime time at which the underlying alarm is triggered in wall time
647         *                    milliseconds since the epoch
648         * @param showIntent an intent that can be used to show or edit details of
649         *                        the alarm clock.
650         */
651        public AlarmClockInfo(long triggerTime, PendingIntent showIntent) {
652            mTriggerTime = triggerTime;
653            mShowIntent = showIntent;
654        }
655
656        /**
657         * Use the {@link #CREATOR}
658         * @hide
659         */
660        AlarmClockInfo(Parcel in) {
661            mTriggerTime = in.readLong();
662            mShowIntent = in.readParcelable(PendingIntent.class.getClassLoader());
663        }
664
665        /**
666         * Returns the time at which the alarm is going to trigger.
667         *
668         * This value is UTC wall clock time in milliseconds, as returned by
669         * {@link System#currentTimeMillis()} for example.
670         */
671        public long getTriggerTime() {
672            return mTriggerTime;
673        }
674
675        /**
676         * Returns an intent intent that can be used to show or edit details of the alarm clock in
677         * the application that scheduled it.
678         *
679         * <p class="note">Beware that any application can retrieve and send this intent,
680         * potentially with additional fields filled in. See
681         * {@link PendingIntent#send(android.content.Context, int, android.content.Intent)
682         * PendingIntent.send()} and {@link android.content.Intent#fillIn Intent.fillIn()}
683         * for details.
684         */
685        public PendingIntent getShowIntent() {
686            return mShowIntent;
687        }
688
689        @Override
690        public int describeContents() {
691            return 0;
692        }
693
694        @Override
695        public void writeToParcel(Parcel dest, int flags) {
696            dest.writeLong(mTriggerTime);
697            dest.writeParcelable(mShowIntent, flags);
698        }
699
700        public static final Creator<AlarmClockInfo> CREATOR = new Creator<AlarmClockInfo>() {
701            @Override
702            public AlarmClockInfo createFromParcel(Parcel in) {
703                return new AlarmClockInfo(in);
704            }
705
706            @Override
707            public AlarmClockInfo[] newArray(int size) {
708                return new AlarmClockInfo[size];
709            }
710        };
711    }
712}
713