blob: cb2130c4528eebb66b3886ce94cc3a7d695b3387 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
Adrian Roosc42a1e12014-07-07 23:35:53 +020019import android.annotation.SdkConstant;
David Christiec20b7952014-09-04 11:29:01 -070020import android.annotation.SystemApi;
Christopher Tatee0a22b32013-07-11 14:43:13 -070021import android.content.Context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.content.Intent;
Christopher Tatee0a22b32013-07-11 14:43:13 -070023import android.os.Build;
Christopher Tate14a7bb02015-10-01 10:24:31 -070024import android.os.Handler;
Jose Lima235510e2014-08-13 12:50:01 -070025import android.os.Parcel;
26import android.os.Parcelable;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027import android.os.RemoteException;
Adrian Roosc42a1e12014-07-07 23:35:53 +020028import android.os.UserHandle;
David Christieebe51fc2013-07-26 13:23:29 -070029import android.os.WorkSource;
Narayan Kamatha78240b2015-04-24 13:22:03 +010030import android.text.TextUtils;
Christopher Tate14a7bb02015-10-01 10:24:31 -070031import android.util.Log;
32
Narayan Kamatha78240b2015-04-24 13:22:03 +010033import libcore.util.ZoneInfoDB;
34
35import java.io.IOException;
Christopher Tate14a7bb02015-10-01 10:24:31 -070036import java.lang.ref.WeakReference;
37import java.util.WeakHashMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038
39/**
40 * This class provides access to the system alarm services. These allow you
41 * to schedule your application to be run at some point in the future. When
42 * an alarm goes off, the {@link Intent} that had been registered for it
43 * is broadcast by the system, automatically starting the target application
44 * if it is not already running. Registered alarms are retained while the
45 * device is asleep (and can optionally wake the device up if they go off
46 * during that time), but will be cleared if it is turned off and rebooted.
Chris Tatea34df8a22009-04-02 23:15:58 -070047 *
48 * <p>The Alarm Manager holds a CPU wake lock as long as the alarm receiver's
49 * onReceive() method is executing. This guarantees that the phone will not sleep
50 * until you have finished handling the broadcast. Once onReceive() returns, the
51 * Alarm Manager releases this wake lock. This means that the phone will in some
52 * cases sleep as soon as your onReceive() method completes. If your alarm receiver
53 * called {@link android.content.Context#startService Context.startService()}, it
54 * is possible that the phone will sleep before the requested service is launched.
55 * To prevent this, your BroadcastReceiver and Service will need to implement a
56 * separate wake lock policy to ensure that the phone continues running until the
57 * service becomes available.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058 *
59 * <p><b>Note: The Alarm Manager is intended for cases where you want to have
60 * your application code run at a specific time, even if your application is
61 * not currently running. For normal timing operations (ticks, timeouts,
62 * etc) it is easier and much more efficient to use
63 * {@link android.os.Handler}.</b>
64 *
Christopher Tate109e4db2013-10-25 16:14:38 -070065 * <p class="caution"><strong>Note:</strong> Beginning with API 19
66 * ({@link android.os.Build.VERSION_CODES#KITKAT}) alarm delivery is inexact:
67 * the OS will shift alarms in order to minimize wakeups and battery use. There are
68 * new APIs to support applications which need strict delivery guarantees; see
69 * {@link #setWindow(int, long, long, PendingIntent)} and
70 * {@link #setExact(int, long, PendingIntent)}. Applications whose {@code targetSdkVersion}
71 * is earlier than API 19 will continue to see the previous behavior in which all
72 * alarms are delivered exactly when requested.
73 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074 * <p>You do not
75 * instantiate this class directly; instead, retrieve it through
76 * {@link android.content.Context#getSystemService
77 * Context.getSystemService(Context.ALARM_SERVICE)}.
78 */
Dianne Hackborn8d66b3f2015-05-08 17:21:48 -070079public class AlarmManager {
Christopher Tate14a7bb02015-10-01 10:24:31 -070080 private static final String TAG = "AlarmManager";
81
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082 /**
83 * Alarm time in {@link System#currentTimeMillis System.currentTimeMillis()}
84 * (wall clock time in UTC), which will wake up the device when
85 * it goes off.
86 */
87 public static final int RTC_WAKEUP = 0;
88 /**
89 * Alarm time in {@link System#currentTimeMillis System.currentTimeMillis()}
90 * (wall clock time in UTC). This alarm does not wake the
91 * device up; if it goes off while the device is asleep, it will not be
92 * delivered until the next time the device wakes up.
93 */
94 public static final int RTC = 1;
95 /**
96 * Alarm time in {@link android.os.SystemClock#elapsedRealtime
97 * SystemClock.elapsedRealtime()} (time since boot, including sleep),
98 * which will wake up the device when it goes off.
99 */
100 public static final int ELAPSED_REALTIME_WAKEUP = 2;
101 /**
102 * Alarm time in {@link android.os.SystemClock#elapsedRealtime
103 * SystemClock.elapsedRealtime()} (time since boot, including sleep).
104 * This alarm does not wake the device up; if it goes off while the device
105 * is asleep, it will not be delivered until the next time the device
106 * wakes up.
107 */
108 public static final int ELAPSED_REALTIME = 3;
109
Adrian Roosc42a1e12014-07-07 23:35:53 +0200110 /**
111 * Broadcast Action: Sent after the value returned by
112 * {@link #getNextAlarmClock()} has changed.
113 *
114 * <p class="note">This is a protected intent that can only be sent by the system.
115 * It is only sent to registered receivers.</p>
116 */
117 @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
118 public static final String ACTION_NEXT_ALARM_CLOCK_CHANGED =
119 "android.app.action.NEXT_ALARM_CLOCK_CHANGED";
120
Christopher Tate57ceaaa2013-07-19 16:30:43 -0700121 /** @hide */
122 public static final long WINDOW_EXACT = 0;
123 /** @hide */
124 public static final long WINDOW_HEURISTIC = -1;
125
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700126 /**
127 * Flag for alarms: this is to be a stand-alone alarm, that should not be batched with
128 * other alarms.
129 * @hide
130 */
131 public static final int FLAG_STANDALONE = 1<<0;
132
133 /**
134 * Flag for alarms: this alarm would like to wake the device even if it is idle. This
135 * is, for example, an alarm for an alarm clock.
136 * @hide
137 */
138 public static final int FLAG_WAKE_FROM_IDLE = 1<<1;
139
140 /**
141 * Flag for alarms: this alarm would like to still execute even if the device is
142 * idle. This won't bring the device out of idle, just allow this specific alarm to
143 * run. Note that this means the actual time this alarm goes off can be inconsistent
144 * with the time of non-allow-while-idle alarms (it could go earlier than the time
145 * requested by another alarm).
146 *
147 * @hide
148 */
149 public static final int FLAG_ALLOW_WHILE_IDLE = 1<<2;
150
151 /**
Dianne Hackborn3d1933c42015-06-10 16:25:57 -0700152 * Flag for alarms: same as {@link #FLAG_ALLOW_WHILE_IDLE}, but doesn't have restrictions
153 * on how frequently it can be scheduled. Only available (and automatically applied) to
154 * system alarms.
155 *
156 * @hide
157 */
158 public static final int FLAG_ALLOW_WHILE_IDLE_UNRESTRICTED = 1<<3;
159
160 /**
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700161 * Flag for alarms: this alarm marks the point where we would like to come out of idle
162 * mode. It may be moved by the alarm manager to match the first wake-from-idle alarm.
163 * Scheduling an alarm with this flag puts the alarm manager in to idle mode, where it
164 * avoids scheduling any further alarms until the marker alarm is executed.
165 * @hide
166 */
Dianne Hackborn3d1933c42015-06-10 16:25:57 -0700167 public static final int FLAG_IDLE_UNTIL = 1<<4;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 private final IAlarmManager mService;
Christopher Tate14a7bb02015-10-01 10:24:31 -0700170 private final String mPackageName;
Christopher Tatee0a22b32013-07-11 14:43:13 -0700171 private final boolean mAlwaysExact;
Narayan Kamatha78240b2015-04-24 13:22:03 +0100172 private final int mTargetSdkVersion;
Christopher Tate14a7bb02015-10-01 10:24:31 -0700173 private final Handler mMainThreadHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174
Christopher Tate14a7bb02015-10-01 10:24:31 -0700175 /**
176 * Direct-notification alarms: the requester must be running continuously from the
177 * time the alarm is set to the time it is delivered, or delivery will fail. Only
178 * one-shot alarms can be set using this mechanism, not repeating alarms.
179 */
180 public interface OnAlarmListener {
181 /**
182 * Callback method that is invoked by the system when the alarm time is reached.
183 */
184 public void onAlarm();
185 }
186
187 final class ListenerWrapper extends IAlarmListener.Stub implements Runnable {
188 final OnAlarmListener mListener;
189 Handler mHandler;
190 IAlarmCompleteListener mCompletion;
191
192 public ListenerWrapper(OnAlarmListener listener) {
193 mListener = listener;
194 }
195
196 public void setHandler(Handler h) {
197 mHandler = h;
198 }
199
200 public void cancel() {
201 try {
202 mService.remove(null, this);
203 } catch (RemoteException ex) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700204 throw ex.rethrowFromSystemServer();
Christopher Tate14a7bb02015-10-01 10:24:31 -0700205 }
206
207 synchronized (AlarmManager.class) {
208 if (sWrappers != null) {
209 sWrappers.remove(mListener);
210 }
211 }
212 }
213
214 @Override
215 public void doAlarm(IAlarmCompleteListener alarmManager) {
216 mCompletion = alarmManager;
217 mHandler.post(this);
218 }
219
220 @Override
221 public void run() {
222 // Remove this listener from the wrapper cache first; the server side
223 // already considers it gone
224 synchronized (AlarmManager.class) {
225 if (sWrappers != null) {
226 sWrappers.remove(mListener);
227 }
228 }
229
230 // Now deliver it to the app
231 try {
232 mListener.onAlarm();
233 } finally {
234 // No catch -- make sure to report completion to the system process,
235 // but continue to allow the exception to crash the app.
236
237 try {
238 mCompletion.alarmComplete(this);
239 } catch (Exception e) {
240 Log.e(TAG, "Unable to report completion to Alarm Manager!", e);
241 }
242 }
243 }
244 }
245
246 // Tracking of the OnAlarmListener -> wrapper mapping, for cancel() support.
247 // Access is synchronized on the AlarmManager class object.
248 //
249 // These are weak references so that we don't leak listener references if, for
250 // example, the pending-alarm messages are posted to a HandlerThread that is
251 // disposed of prior to alarm delivery. The underlying messages will be GC'd
252 // but this static reference would still persist, orphaned, never deallocated.
253 private static WeakHashMap<OnAlarmListener, WeakReference<ListenerWrapper>> sWrappers;
Christopher Tate57ceaaa2013-07-19 16:30:43 -0700254
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 /**
256 * package private on purpose
257 */
Christopher Tatee0a22b32013-07-11 14:43:13 -0700258 AlarmManager(IAlarmManager service, Context ctx) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259 mService = service;
Christopher Tatee0a22b32013-07-11 14:43:13 -0700260
Christopher Tate14a7bb02015-10-01 10:24:31 -0700261 mPackageName = ctx.getPackageName();
Narayan Kamatha78240b2015-04-24 13:22:03 +0100262 mTargetSdkVersion = ctx.getApplicationInfo().targetSdkVersion;
263 mAlwaysExact = (mTargetSdkVersion < Build.VERSION_CODES.KITKAT);
Christopher Tate14a7bb02015-10-01 10:24:31 -0700264 mMainThreadHandler = new Handler(ctx.getMainLooper());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 }
Christopher Tate57ceaaa2013-07-19 16:30:43 -0700266
267 private long legacyExactLength() {
268 return (mAlwaysExact ? WINDOW_EXACT : WINDOW_HEURISTIC);
269 }
270
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800271 /**
Christopher Tate57ceaaa2013-07-19 16:30:43 -0700272 * <p>Schedule an alarm. <b>Note: for timing operations (ticks, timeouts,
Christopher Tate062bce72013-10-25 13:59:44 -0700273 * etc) it is easier and much more efficient to use {@link android.os.Handler}.</b>
274 * If there is already an alarm scheduled for the same IntentSender, that previous
275 * alarm will first be canceled.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276 *
Christopher Tate062bce72013-10-25 13:59:44 -0700277 * <p>If the stated trigger time is in the past, the alarm will be triggered
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800278 * immediately. If there is already an alarm for this Intent
279 * scheduled (with the equality of two intents being defined by
280 * {@link Intent#filterEquals}), then it will be removed and replaced by
281 * this one.
282 *
283 * <p>
Christopher Tate062bce72013-10-25 13:59:44 -0700284 * The alarm is an Intent broadcast that goes to a broadcast receiver that
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800285 * you registered with {@link android.content.Context#registerReceiver}
286 * or through the &lt;receiver&gt; tag in an AndroidManifest.xml file.
287 *
288 * <p>
289 * Alarm intents are delivered with a data extra of type int called
290 * {@link Intent#EXTRA_ALARM_COUNT Intent.EXTRA_ALARM_COUNT} that indicates
291 * how many past alarm events have been accumulated into this intent
292 * broadcast. Recurring alarms that have gone undelivered because the
293 * phone was asleep may have a count greater than one when delivered.
Christopher Tate062bce72013-10-25 13:59:44 -0700294 *
Christopher Tate109e4db2013-10-25 16:14:38 -0700295 * <div class="note">
Christopher Tate062bce72013-10-25 13:59:44 -0700296 * <p>
297 * <b>Note:</b> Beginning in API 19, the trigger time passed to this method
298 * is treated as inexact: the alarm will not be delivered before this time, but
299 * may be deferred and delivered some time later. The OS will use
300 * this policy in order to "batch" alarms together across the entire system,
301 * minimizing the number of times the device needs to "wake up" and minimizing
302 * battery use. In general, alarms scheduled in the near future will not
303 * be deferred as long as alarms scheduled far in the future.
304 *
305 * <p>
306 * With the new batching policy, delivery ordering guarantees are not as
307 * strong as they were previously. If the application sets multiple alarms,
Christopher Tate109e4db2013-10-25 16:14:38 -0700308 * it is possible that these alarms' <em>actual</em> delivery ordering may not match
309 * the order of their <em>requested</em> delivery times. If your application has
Christopher Tate062bce72013-10-25 13:59:44 -0700310 * strong ordering requirements there are other APIs that you can use to get
311 * the necessary behavior; see {@link #setWindow(int, long, long, PendingIntent)}
312 * and {@link #setExact(int, long, PendingIntent)}.
313 *
314 * <p>
Christopher Tate109e4db2013-10-25 16:14:38 -0700315 * Applications whose {@code targetSdkVersion} is before API 19 will
Christopher Tate062bce72013-10-25 13:59:44 -0700316 * continue to get the previous alarm behavior: all of their scheduled alarms
317 * will be treated as exact.
Christopher Tate109e4db2013-10-25 16:14:38 -0700318 * </div>
Christopher Tate062bce72013-10-25 13:59:44 -0700319 *
320 * @param type One of {@link #ELAPSED_REALTIME}, {@link #ELAPSED_REALTIME_WAKEUP},
321 * {@link #RTC}, or {@link #RTC_WAKEUP}.
Jesse Wilson79074cd2011-12-22 22:51:37 -0500322 * @param triggerAtMillis time in milliseconds that the alarm should go
323 * off, using the appropriate clock (depending on the alarm type).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800324 * @param operation Action to perform when the alarm goes off;
325 * typically comes from {@link PendingIntent#getBroadcast
326 * IntentSender.getBroadcast()}.
327 *
328 * @see android.os.Handler
Christopher Tate57ceaaa2013-07-19 16:30:43 -0700329 * @see #setExact
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800330 * @see #setRepeating
Christopher Tate57ceaaa2013-07-19 16:30:43 -0700331 * @see #setWindow
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800332 * @see #cancel
333 * @see android.content.Context#sendBroadcast
334 * @see android.content.Context#registerReceiver
335 * @see android.content.Intent#filterEquals
336 * @see #ELAPSED_REALTIME
337 * @see #ELAPSED_REALTIME_WAKEUP
338 * @see #RTC
339 * @see #RTC_WAKEUP
340 */
Jesse Wilson79074cd2011-12-22 22:51:37 -0500341 public void set(int type, long triggerAtMillis, PendingIntent operation) {
Christopher Tate14a7bb02015-10-01 10:24:31 -0700342 setImpl(type, triggerAtMillis, legacyExactLength(), 0, 0, operation, null, null,
343 null, null, null);
344 }
345
346 /**
347 * Direct callback version of {@link #set(int, long, PendingIntent)}. Rather than
348 * supplying a PendingIntent to be sent when the alarm time is reached, this variant
349 * supplies an {@link OnAlarmListener} instance that will be invoked at that time.
350 * <p>
351 * The OnAlarmListener's {@link OnAlarmListener#onAlarm() onAlarm()} method will be
352 * invoked via the specified target Handler, or on the application's main looper
353 * if {@code null} is passed as the {@code targetHandler} parameter.
354 *
355 * @param type One of {@link #ELAPSED_REALTIME}, {@link #ELAPSED_REALTIME_WAKEUP},
356 * {@link #RTC}, or {@link #RTC_WAKEUP}.
357 * @param triggerAtMillis time in milliseconds that the alarm should go
358 * off, using the appropriate clock (depending on the alarm type).
359 * @param tag string describing the alarm, used for logging and battery-use
360 * attribution
361 * @param listener {@link OnAlarmListener} instance whose
362 * {@link OnAlarmListener#onAlarm() onAlarm()} method will be
363 * called when the alarm time is reached. A given OnAlarmListener instance can
364 * only be the target of a single pending alarm, just as a given PendingIntent
365 * can only be used with one alarm at a time.
366 * @param targetHandler {@link Handler} on which to execute the listener's onAlarm()
367 * callback, or {@code null} to run that callback on the main looper.
368 */
369 public void set(int type, long triggerAtMillis, String tag, OnAlarmListener listener,
370 Handler targetHandler) {
371 setImpl(type, triggerAtMillis, legacyExactLength(), 0, 0, null, listener, tag,
372 targetHandler, null, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800373 }
374
375 /**
376 * Schedule a repeating alarm. <b>Note: for timing operations (ticks,
377 * timeouts, etc) it is easier and much more efficient to use
378 * {@link android.os.Handler}.</b> If there is already an alarm scheduled
379 * for the same IntentSender, it will first be canceled.
380 *
Christopher Tate062bce72013-10-25 13:59:44 -0700381 * <p>Like {@link #set}, except you can also supply a period at which
382 * the alarm will automatically repeat. This alarm continues
383 * repeating until explicitly removed with {@link #cancel}. If the stated
384 * trigger time is in the past, the alarm will be triggered immediately, with an
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800385 * alarm count depending on how far in the past the trigger time is relative
386 * to the repeat interval.
387 *
388 * <p>If an alarm is delayed (by system sleep, for example, for non
389 * _WAKEUP alarm types), a skipped repeat will be delivered as soon as
390 * possible. After that, future alarms will be delivered according to the
391 * original schedule; they do not drift over time. For example, if you have
392 * set a recurring alarm for the top of every hour but the phone was asleep
393 * from 7:45 until 8:45, an alarm will be sent as soon as the phone awakens,
394 * then the next alarm will be sent at 9:00.
395 *
396 * <p>If your application wants to allow the delivery times to drift in
397 * order to guarantee that at least a certain time interval always elapses
398 * between alarms, then the approach to take is to use one-time alarms,
399 * scheduling the next one yourself when handling each alarm delivery.
400 *
Christopher Tate109e4db2013-10-25 16:14:38 -0700401 * <p class="note">
Christopher Tate062bce72013-10-25 13:59:44 -0700402 * <b>Note:</b> as of API 19, all repeating alarms are inexact. If your
403 * application needs precise delivery times then it must use one-time
404 * exact alarms, rescheduling each time as described above. Legacy applications
Christopher Tate109e4db2013-10-25 16:14:38 -0700405 * whose {@code targetSdkVersion} is earlier than API 19 will continue to have all
Christopher Tate062bce72013-10-25 13:59:44 -0700406 * of their alarms, including repeating alarms, treated as exact.
407 *
408 * @param type One of {@link #ELAPSED_REALTIME}, {@link #ELAPSED_REALTIME_WAKEUP},
409 * {@link #RTC}, or {@link #RTC_WAKEUP}.
Jesse Wilson79074cd2011-12-22 22:51:37 -0500410 * @param triggerAtMillis time in milliseconds that the alarm should first
411 * go off, using the appropriate clock (depending on the alarm type).
412 * @param intervalMillis interval in milliseconds between subsequent repeats
413 * of the alarm.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800414 * @param operation Action to perform when the alarm goes off;
415 * typically comes from {@link PendingIntent#getBroadcast
416 * IntentSender.getBroadcast()}.
417 *
418 * @see android.os.Handler
419 * @see #set
Christopher Tate57ceaaa2013-07-19 16:30:43 -0700420 * @see #setExact
421 * @see #setWindow
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422 * @see #cancel
423 * @see android.content.Context#sendBroadcast
424 * @see android.content.Context#registerReceiver
425 * @see android.content.Intent#filterEquals
426 * @see #ELAPSED_REALTIME
427 * @see #ELAPSED_REALTIME_WAKEUP
428 * @see #RTC
429 * @see #RTC_WAKEUP
430 */
Jesse Wilson79074cd2011-12-22 22:51:37 -0500431 public void setRepeating(int type, long triggerAtMillis,
432 long intervalMillis, PendingIntent operation) {
Christopher Tate14a7bb02015-10-01 10:24:31 -0700433 setImpl(type, triggerAtMillis, legacyExactLength(), intervalMillis, 0, operation,
434 null, null, null, null, null);
Christopher Tate57ceaaa2013-07-19 16:30:43 -0700435 }
436
437 /**
Christopher Tate062bce72013-10-25 13:59:44 -0700438 * Schedule an alarm to be delivered within a given window of time. This method
439 * is similar to {@link #set(int, long, PendingIntent)}, but allows the
440 * application to precisely control the degree to which its delivery might be
441 * adjusted by the OS. This method allows an application to take advantage of the
442 * battery optimizations that arise from delivery batching even when it has
443 * modest timeliness requirements for its alarms.
Christopher Tate57ceaaa2013-07-19 16:30:43 -0700444 *
Christopher Tate062bce72013-10-25 13:59:44 -0700445 * <p>
Christopher Tate109e4db2013-10-25 16:14:38 -0700446 * This method can also be used to achieve strict ordering guarantees among
447 * multiple alarms by ensuring that the windows requested for each alarm do
448 * not intersect.
Christopher Tate57ceaaa2013-07-19 16:30:43 -0700449 *
Christopher Tate062bce72013-10-25 13:59:44 -0700450 * <p>
451 * When precise delivery is not required, applications should use the standard
452 * {@link #set(int, long, PendingIntent)} method. This will give the OS the most
Christopher Tate109e4db2013-10-25 16:14:38 -0700453 * flexibility to minimize wakeups and battery use. For alarms that must be delivered
Christopher Tate062bce72013-10-25 13:59:44 -0700454 * at precisely-specified times with no acceptable variation, applications can use
455 * {@link #setExact(int, long, PendingIntent)}.
456 *
457 * @param type One of {@link #ELAPSED_REALTIME}, {@link #ELAPSED_REALTIME_WAKEUP},
Christopher Tate109e4db2013-10-25 16:14:38 -0700458 * {@link #RTC}, or {@link #RTC_WAKEUP}.
Christopher Tate57ceaaa2013-07-19 16:30:43 -0700459 * @param windowStartMillis The earliest time, in milliseconds, that the alarm should
460 * be delivered, expressed in the appropriate clock's units (depending on the alarm
461 * type).
462 * @param windowLengthMillis The length of the requested delivery window,
463 * in milliseconds. The alarm will be delivered no later than this many
Christopher Tate062bce72013-10-25 13:59:44 -0700464 * milliseconds after {@code windowStartMillis}. Note that this parameter
Christopher Tate57ceaaa2013-07-19 16:30:43 -0700465 * is a <i>duration,</i> not the timestamp of the end of the window.
466 * @param operation Action to perform when the alarm goes off;
467 * typically comes from {@link PendingIntent#getBroadcast
468 * IntentSender.getBroadcast()}.
469 *
470 * @see #set
471 * @see #setExact
472 * @see #setRepeating
473 * @see #cancel
474 * @see android.content.Context#sendBroadcast
475 * @see android.content.Context#registerReceiver
476 * @see android.content.Intent#filterEquals
477 * @see #ELAPSED_REALTIME
478 * @see #ELAPSED_REALTIME_WAKEUP
479 * @see #RTC
480 * @see #RTC_WAKEUP
481 */
482 public void setWindow(int type, long windowStartMillis, long windowLengthMillis,
483 PendingIntent operation) {
Christopher Tate14a7bb02015-10-01 10:24:31 -0700484 setImpl(type, windowStartMillis, windowLengthMillis, 0, 0, operation,
485 null, null, null, null, null);
486 }
487
488 /**
489 * Direct callback version of {@link #setWindow(int, long, long, PendingIntent)}. Rather
490 * than supplying a PendingIntent to be sent when the alarm time is reached, this variant
491 * supplies an {@link OnAlarmListener} instance that will be invoked at that time.
492 * <p>
493 * The OnAlarmListener {@link OnAlarmListener#onAlarm() onAlarm()} method will be
494 * invoked via the specified target Handler, or on the application's main looper
495 * if {@code null} is passed as the {@code targetHandler} parameter.
496 */
497 public void setWindow(int type, long windowStartMillis, long windowLengthMillis,
498 String tag, OnAlarmListener listener, Handler targetHandler) {
499 setImpl(type, windowStartMillis, windowLengthMillis, 0, 0, null, listener, tag,
500 targetHandler, null, null);
Christopher Tatee0a22b32013-07-11 14:43:13 -0700501 }
502
503 /**
Christopher Tate062bce72013-10-25 13:59:44 -0700504 * Schedule an alarm to be delivered precisely at the stated time.
505 *
506 * <p>
507 * This method is like {@link #set(int, long, PendingIntent)}, but does not permit
508 * the OS to adjust the delivery time. The alarm will be delivered as nearly as
509 * possible to the requested trigger time.
510 *
511 * <p>
512 * <b>Note:</b> only alarms for which there is a strong demand for exact-time
513 * delivery (such as an alarm clock ringing at the requested time) should be
514 * scheduled as exact. Applications are strongly discouraged from using exact
515 * alarms unnecessarily as they reduce the OS's ability to minimize battery use.
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 go
520 * off, using the appropriate clock (depending on the alarm type).
521 * @param operation Action to perform when the alarm goes off;
522 * typically comes from {@link PendingIntent#getBroadcast
523 * IntentSender.getBroadcast()}.
524 *
525 * @see #set
526 * @see #setRepeating
527 * @see #setWindow
528 * @see #cancel
529 * @see android.content.Context#sendBroadcast
530 * @see android.content.Context#registerReceiver
531 * @see android.content.Intent#filterEquals
532 * @see #ELAPSED_REALTIME
533 * @see #ELAPSED_REALTIME_WAKEUP
534 * @see #RTC
535 * @see #RTC_WAKEUP
Christopher Tatee0a22b32013-07-11 14:43:13 -0700536 */
537 public void setExact(int type, long triggerAtMillis, PendingIntent operation) {
Christopher Tate14a7bb02015-10-01 10:24:31 -0700538 setImpl(type, triggerAtMillis, WINDOW_EXACT, 0, 0, operation, null, null, null,
539 null, null);
540 }
541
542 /**
543 * Direct callback version of {@link #setExact(int, long, PendingIntent)}. Rather
544 * than supplying a PendingIntent to be sent when the alarm time is reached, this variant
545 * supplies an {@link OnAlarmListener} instance that will be invoked at that time.
546 * <p>
547 * The OnAlarmListener's {@link OnAlarmListener#onAlarm() onAlarm()} method will be
548 * invoked via the specified target Handler, or on the application's main looper
549 * if {@code null} is passed as the {@code targetHandler} parameter.
550 */
551 public void setExact(int type, long triggerAtMillis, String tag, OnAlarmListener listener,
552 Handler targetHandler) {
553 setImpl(type, triggerAtMillis, WINDOW_EXACT, 0, 0, null, listener, tag,
554 targetHandler, null, null);
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700555 }
556
557 /**
558 * Schedule an idle-until alarm, which will keep the alarm manager idle until
559 * the given time.
560 * @hide
561 */
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -0700562 public void setIdleUntil(int type, long triggerAtMillis, String tag, OnAlarmListener listener,
563 Handler targetHandler) {
564 setImpl(type, triggerAtMillis, WINDOW_EXACT, 0, FLAG_IDLE_UNTIL, null,
565 listener, tag, targetHandler, null, null);
Adrian Roosc42a1e12014-07-07 23:35:53 +0200566 }
567
568 /**
569 * Schedule an alarm that represents an alarm clock.
570 *
571 * The system may choose to display information about this alarm to the user.
572 *
573 * <p>
574 * This method is like {@link #setExact(int, long, PendingIntent)}, but implies
575 * {@link #RTC_WAKEUP}.
576 *
577 * @param info
578 * @param operation Action to perform when the alarm goes off;
579 * typically comes from {@link PendingIntent#getBroadcast
580 * IntentSender.getBroadcast()}.
581 *
582 * @see #set
583 * @see #setRepeating
584 * @see #setWindow
585 * @see #setExact
586 * @see #cancel
587 * @see #getNextAlarmClock()
588 * @see android.content.Context#sendBroadcast
589 * @see android.content.Context#registerReceiver
590 * @see android.content.Intent#filterEquals
591 */
592 public void setAlarmClock(AlarmClockInfo info, PendingIntent operation) {
Christopher Tate14a7bb02015-10-01 10:24:31 -0700593 setImpl(RTC_WAKEUP, info.getTriggerTime(), WINDOW_EXACT, 0, 0, operation,
594 null, null, null, null, info);
David Christieebe51fc2013-07-26 13:23:29 -0700595 }
596
597 /** @hide */
David Christiec20b7952014-09-04 11:29:01 -0700598 @SystemApi
David Christieebe51fc2013-07-26 13:23:29 -0700599 public void set(int type, long triggerAtMillis, long windowMillis, long intervalMillis,
600 PendingIntent operation, WorkSource workSource) {
Christopher Tate14a7bb02015-10-01 10:24:31 -0700601 setImpl(type, triggerAtMillis, windowMillis, intervalMillis, 0, operation, null, null,
602 null, workSource, null);
603 }
604
605 /**
606 * Direct callback version of {@link #set(int, long, long, long, PendingIntent, WorkSource)}.
607 * Note that repeating alarms must use the PendingIntent variant, not an OnAlarmListener.
608 * <p>
609 * The OnAlarmListener's {@link OnAlarmListener#onAlarm() onAlarm()} method will be
610 * invoked via the specified target Handler, or on the application's main looper
611 * if {@code null} is passed as the {@code targetHandler} parameter.
612 *
613 * @hide
614 */
Christopher Tatef2d753e2015-11-04 11:10:48 -0800615 @SystemApi
Christopher Tate14a7bb02015-10-01 10:24:31 -0700616 public void set(int type, long triggerAtMillis, long windowMillis, long intervalMillis,
617 OnAlarmListener listener, Handler targetHandler, WorkSource workSource) {
618 setImpl(type, triggerAtMillis, windowMillis, intervalMillis, 0, null, listener, null,
619 targetHandler, workSource, null);
Christopher Tatee0a22b32013-07-11 14:43:13 -0700620 }
621
Christopher Tate57ceaaa2013-07-19 16:30:43 -0700622 private void setImpl(int type, long triggerAtMillis, long windowMillis, long intervalMillis,
Christopher Tate14a7bb02015-10-01 10:24:31 -0700623 int flags, PendingIntent operation, final OnAlarmListener listener, String listenerTag,
624 Handler targetHandler, WorkSource workSource, AlarmClockInfo alarmClock) {
Christopher Tate5f221e82013-07-30 17:13:15 -0700625 if (triggerAtMillis < 0) {
Christopher Tate56cfa242013-07-30 19:09:41 -0700626 /* NOTYET
Christopher Tate5f221e82013-07-30 17:13:15 -0700627 if (mAlwaysExact) {
628 // Fatal error for KLP+ apps to use negative trigger times
629 throw new IllegalArgumentException("Invalid alarm trigger time "
630 + triggerAtMillis);
631 }
Christopher Tate56cfa242013-07-30 19:09:41 -0700632 */
Christopher Tate5f221e82013-07-30 17:13:15 -0700633 triggerAtMillis = 0;
634 }
David Christieebe51fc2013-07-26 13:23:29 -0700635
Christopher Tate14a7bb02015-10-01 10:24:31 -0700636 ListenerWrapper recipientWrapper = null;
637 if (listener != null) {
638 synchronized (AlarmManager.class) {
639 if (sWrappers == null) {
640 sWrappers = new WeakHashMap<OnAlarmListener, WeakReference<ListenerWrapper>>();
641 }
642
643 WeakReference<ListenerWrapper> wrapperRef = sWrappers.get(listener);
644 // no existing wrapper *or* we've lost our weak ref to it => build a new one
645 if (wrapperRef == null ||
646 (recipientWrapper = wrapperRef.get()) == null) {
647 recipientWrapper = new ListenerWrapper(listener);
648 wrapperRef = new WeakReference<ListenerWrapper>(recipientWrapper);
649 sWrappers.put(listener, wrapperRef);
650 }
651 }
652
653 final Handler handler = (targetHandler != null) ? targetHandler : mMainThreadHandler;
654 recipientWrapper.setHandler(handler);
655 }
656
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800657 try {
Christopher Tate14a7bb02015-10-01 10:24:31 -0700658 mService.set(mPackageName, type, triggerAtMillis, windowMillis, intervalMillis, flags,
659 operation, recipientWrapper, listenerTag, workSource, alarmClock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800660 } catch (RemoteException ex) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700661 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800662 }
663 }
664
665 /**
Christopher Tate109e4db2013-10-25 16:14:38 -0700666 * Available inexact recurrence interval recognized by
667 * {@link #setInexactRepeating(int, long, long, PendingIntent)}
668 * when running on Android prior to API 19.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800669 */
670 public static final long INTERVAL_FIFTEEN_MINUTES = 15 * 60 * 1000;
Christopher Tatee0a22b32013-07-11 14:43:13 -0700671
672 /**
Christopher Tate109e4db2013-10-25 16:14:38 -0700673 * Available inexact recurrence interval recognized by
674 * {@link #setInexactRepeating(int, long, long, PendingIntent)}
675 * when running on Android prior to API 19.
Christopher Tatee0a22b32013-07-11 14:43:13 -0700676 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800677 public static final long INTERVAL_HALF_HOUR = 2*INTERVAL_FIFTEEN_MINUTES;
Christopher Tatee0a22b32013-07-11 14:43:13 -0700678
679 /**
Christopher Tate109e4db2013-10-25 16:14:38 -0700680 * Available inexact recurrence interval recognized by
681 * {@link #setInexactRepeating(int, long, long, PendingIntent)}
682 * when running on Android prior to API 19.
Christopher Tatee0a22b32013-07-11 14:43:13 -0700683 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800684 public static final long INTERVAL_HOUR = 2*INTERVAL_HALF_HOUR;
Christopher Tatee0a22b32013-07-11 14:43:13 -0700685
686 /**
Christopher Tate109e4db2013-10-25 16:14:38 -0700687 * Available inexact recurrence interval recognized by
688 * {@link #setInexactRepeating(int, long, long, PendingIntent)}
689 * when running on Android prior to API 19.
Christopher Tatee0a22b32013-07-11 14:43:13 -0700690 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800691 public static final long INTERVAL_HALF_DAY = 12*INTERVAL_HOUR;
Christopher Tatee0a22b32013-07-11 14:43:13 -0700692
693 /**
Christopher Tate109e4db2013-10-25 16:14:38 -0700694 * Available inexact recurrence interval recognized by
695 * {@link #setInexactRepeating(int, long, long, PendingIntent)}
696 * when running on Android prior to API 19.
Christopher Tatee0a22b32013-07-11 14:43:13 -0700697 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800698 public static final long INTERVAL_DAY = 2*INTERVAL_HALF_DAY;
Christopher Tatee0a22b32013-07-11 14:43:13 -0700699
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800700 /**
701 * Schedule a repeating alarm that has inexact trigger time requirements;
702 * for example, an alarm that repeats every hour, but not necessarily at
703 * the top of every hour. These alarms are more power-efficient than
Christopher Tate109e4db2013-10-25 16:14:38 -0700704 * the strict recurrences traditionally supplied by {@link #setRepeating}, since the
705 * system can adjust alarms' delivery times to cause them to fire simultaneously,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800706 * avoiding waking the device from sleep more than necessary.
Christopher Tate109e4db2013-10-25 16:14:38 -0700707 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800708 * <p>Your alarm's first trigger will not be before the requested time,
709 * but it might not occur for almost a full interval after that time. In
710 * addition, while the overall period of the repeating alarm will be as
711 * requested, the time between any two successive firings of the alarm
712 * may vary. If your application demands very low jitter, use
Christopher Tate109e4db2013-10-25 16:14:38 -0700713 * one-shot alarms with an appropriate window instead; see {@link
714 * #setWindow(int, long, long, PendingIntent)} and
715 * {@link #setExact(int, long, PendingIntent)}.
716 *
717 * <p class="note">
718 * As of API 19, all repeating alarms are inexact. Because this method has
719 * been available since API 3, your application can safely call it and be
720 * assured that it will get similar behavior on both current and older versions
721 * of Android.
Jesse Wilson79074cd2011-12-22 22:51:37 -0500722 *
Christopher Tate062bce72013-10-25 13:59:44 -0700723 * @param type One of {@link #ELAPSED_REALTIME}, {@link #ELAPSED_REALTIME_WAKEUP},
724 * {@link #RTC}, or {@link #RTC_WAKEUP}.
Jesse Wilson79074cd2011-12-22 22:51:37 -0500725 * @param triggerAtMillis time in milliseconds that the alarm should first
726 * go off, using the appropriate clock (depending on the alarm type). This
727 * is inexact: the alarm will not fire before this time, but there may be a
728 * delay of almost an entire alarm interval before the first invocation of
729 * the alarm.
730 * @param intervalMillis interval in milliseconds between subsequent repeats
Christopher Tate109e4db2013-10-25 16:14:38 -0700731 * of the alarm. Prior to API 19, if this is one of INTERVAL_FIFTEEN_MINUTES,
Jesse Wilson79074cd2011-12-22 22:51:37 -0500732 * INTERVAL_HALF_HOUR, INTERVAL_HOUR, INTERVAL_HALF_DAY, or INTERVAL_DAY
733 * then the alarm will be phase-aligned with other alarms to reduce the
734 * number of wakeups. Otherwise, the alarm will be set as though the
Christopher Tate109e4db2013-10-25 16:14:38 -0700735 * application had called {@link #setRepeating}. As of API 19, all repeating
736 * alarms will be inexact and subject to batching with other alarms regardless
737 * of their stated repeat interval.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800738 * @param operation Action to perform when the alarm goes off;
739 * typically comes from {@link PendingIntent#getBroadcast
740 * IntentSender.getBroadcast()}.
741 *
742 * @see android.os.Handler
743 * @see #set
744 * @see #cancel
745 * @see android.content.Context#sendBroadcast
746 * @see android.content.Context#registerReceiver
747 * @see android.content.Intent#filterEquals
748 * @see #ELAPSED_REALTIME
749 * @see #ELAPSED_REALTIME_WAKEUP
750 * @see #RTC
751 * @see #RTC_WAKEUP
752 * @see #INTERVAL_FIFTEEN_MINUTES
753 * @see #INTERVAL_HALF_HOUR
754 * @see #INTERVAL_HOUR
755 * @see #INTERVAL_HALF_DAY
756 * @see #INTERVAL_DAY
757 */
Jesse Wilson79074cd2011-12-22 22:51:37 -0500758 public void setInexactRepeating(int type, long triggerAtMillis,
759 long intervalMillis, PendingIntent operation) {
Christopher Tate14a7bb02015-10-01 10:24:31 -0700760 setImpl(type, triggerAtMillis, WINDOW_HEURISTIC, intervalMillis, 0, operation, null,
761 null, null, null, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800762 }
Dianne Hackborn8d66b3f2015-05-08 17:21:48 -0700763
764 /**
765 * Like {@link #set(int, long, PendingIntent)}, but this alarm will be allowed to execute
766 * even when the system is in low-power idle modes. This type of alarm must <b>only</b>
767 * be used for situations where it is actually required that the alarm go off while in
768 * idle -- a reasonable example would be for a calendar notification that should make a
Dianne Hackborn14c5ab42015-07-09 18:17:54 -0700769 * sound so the user is aware of it. When the alarm is dispatched, the app will also be
770 * added to the system's temporary whitelist for approximately 10 seconds to allow that
771 * application to acquire further wake locks in which to complete its work.</p>
Dianne Hackborn8d66b3f2015-05-08 17:21:48 -0700772 *
Dianne Hackborn14c5ab42015-07-09 18:17:54 -0700773 * <p>These alarms can significantly impact the power use
774 * of the device when idle (and thus cause significant battery blame to the app scheduling
775 * them), so they should be used with care. To reduce abuse, there are restrictions on how
776 * frequently these alarms will go off for a particular application.
777 * Under normal system operation, it will not dispatch these
Dianne Hackborn3d1933c42015-06-10 16:25:57 -0700778 * alarms more than about every minute (at which point every such pending alarm is
779 * dispatched); when in low-power idle modes this duration may be significantly longer,
780 * such as 15 minutes.</p>
781 *
Dianne Hackborn8d66b3f2015-05-08 17:21:48 -0700782 * <p>Unlike other alarms, the system is free to reschedule this type of alarm to happen
783 * out of order with any other alarms, even those from the same app. This will clearly happen
784 * when the device is idle (since this alarm can go off while idle, when any other alarms
785 * from the app will be held until later), but may also happen even when not idle.</p>
786 *
787 * <p>Regardless of the app's target SDK version, this call always allows batching of the
788 * alarm.</p>
789 *
790 * @param type One of {@link #ELAPSED_REALTIME}, {@link #ELAPSED_REALTIME_WAKEUP},
791 * {@link #RTC}, or {@link #RTC_WAKEUP}.
792 * @param triggerAtMillis time in milliseconds that the alarm should go
793 * off, using the appropriate clock (depending on the alarm type).
794 * @param operation Action to perform when the alarm goes off;
795 * typically comes from {@link PendingIntent#getBroadcast
796 * IntentSender.getBroadcast()}.
797 *
798 * @see #set(int, long, PendingIntent)
799 * @see #setExactAndAllowWhileIdle
800 * @see #cancel
801 * @see android.content.Context#sendBroadcast
802 * @see android.content.Context#registerReceiver
803 * @see android.content.Intent#filterEquals
804 * @see #ELAPSED_REALTIME
805 * @see #ELAPSED_REALTIME_WAKEUP
806 * @see #RTC
807 * @see #RTC_WAKEUP
808 */
809 public void setAndAllowWhileIdle(int type, long triggerAtMillis, PendingIntent operation) {
Christopher Tate14a7bb02015-10-01 10:24:31 -0700810 setImpl(type, triggerAtMillis, WINDOW_HEURISTIC, 0, FLAG_ALLOW_WHILE_IDLE,
811 operation, null, null, null, null, null);
Dianne Hackborn8d66b3f2015-05-08 17:21:48 -0700812 }
813
814 /**
815 * Like {@link #setExact(int, long, PendingIntent)}, but this alarm will be allowed to execute
816 * even when the system is in low-power idle modes. If you don't need exact scheduling of
817 * the alarm but still need to execute while idle, consider using
818 * {@link #setAndAllowWhileIdle}. This type of alarm must <b>only</b>
819 * be used for situations where it is actually required that the alarm go off while in
820 * idle -- a reasonable example would be for a calendar notification that should make a
Dianne Hackborn14c5ab42015-07-09 18:17:54 -0700821 * sound so the user is aware of it. When the alarm is dispatched, the app will also be
822 * added to the system's temporary whitelist for approximately 10 seconds to allow that
823 * application to acquire further wake locks in which to complete its work.</p>
Dianne Hackborn8d66b3f2015-05-08 17:21:48 -0700824 *
Dianne Hackborn14c5ab42015-07-09 18:17:54 -0700825 * <p>These alarms can significantly impact the power use
826 * of the device when idle (and thus cause significant battery blame to the app scheduling
827 * them), so they should be used with care. To reduce abuse, there are restrictions on how
828 * frequently these alarms will go off for a particular application.
829 * Under normal system operation, it will not dispatch these
Dianne Hackborn3d1933c42015-06-10 16:25:57 -0700830 * alarms more than about every minute (at which point every such pending alarm is
831 * dispatched); when in low-power idle modes this duration may be significantly longer,
832 * such as 15 minutes.</p>
833 *
Dianne Hackborn8d66b3f2015-05-08 17:21:48 -0700834 * <p>Unlike other alarms, the system is free to reschedule this type of alarm to happen
835 * out of order with any other alarms, even those from the same app. This will clearly happen
836 * when the device is idle (since this alarm can go off while idle, when any other alarms
837 * from the app will be held until later), but may also happen even when not idle.
838 * Note that the OS will allow itself more flexibility for scheduling these alarms than
839 * regular exact alarms, since the application has opted into this behavior. When the
840 * device is idle it may take even more liberties with scheduling in order to optimize
841 * for battery life.</p>
842 *
843 * @param type One of {@link #ELAPSED_REALTIME}, {@link #ELAPSED_REALTIME_WAKEUP},
844 * {@link #RTC}, or {@link #RTC_WAKEUP}.
845 * @param triggerAtMillis time in milliseconds that the alarm should go
846 * off, using the appropriate clock (depending on the alarm type).
847 * @param operation Action to perform when the alarm goes off;
848 * typically comes from {@link PendingIntent#getBroadcast
849 * IntentSender.getBroadcast()}.
850 *
851 * @see #set
852 * @see #setRepeating
853 * @see #setWindow
854 * @see #cancel
855 * @see android.content.Context#sendBroadcast
856 * @see android.content.Context#registerReceiver
857 * @see android.content.Intent#filterEquals
858 * @see #ELAPSED_REALTIME
859 * @see #ELAPSED_REALTIME_WAKEUP
860 * @see #RTC
861 * @see #RTC_WAKEUP
862 */
863 public void setExactAndAllowWhileIdle(int type, long triggerAtMillis, PendingIntent operation) {
864 setImpl(type, triggerAtMillis, WINDOW_EXACT, 0, FLAG_ALLOW_WHILE_IDLE, operation,
Christopher Tate14a7bb02015-10-01 10:24:31 -0700865 null, null, null, null, null);
Dianne Hackborn8d66b3f2015-05-08 17:21:48 -0700866 }
867
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800868 /**
869 * Remove any alarms with a matching {@link Intent}.
870 * Any alarm, of any type, whose Intent matches this one (as defined by
871 * {@link Intent#filterEquals}), will be canceled.
872 *
873 * @param operation IntentSender which matches a previously added
Christopher Tatebb76a6c2015-12-02 10:54:56 -0800874 * IntentSender. This parameter must not be {@code null}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800875 *
876 * @see #set
877 */
878 public void cancel(PendingIntent operation) {
Christopher Tate14a7bb02015-10-01 10:24:31 -0700879 if (operation == null) {
Christopher Tatebb76a6c2015-12-02 10:54:56 -0800880 final String msg = "cancel() called with a null PendingIntent";
881 if (mTargetSdkVersion >= Build.VERSION_CODES.N) {
882 throw new NullPointerException(msg);
883 } else {
884 Log.e(TAG, msg);
885 return;
886 }
Christopher Tate14a7bb02015-10-01 10:24:31 -0700887 }
888
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800889 try {
Christopher Tate14a7bb02015-10-01 10:24:31 -0700890 mService.remove(operation, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800891 } catch (RemoteException ex) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700892 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800893 }
894 }
Dan Egnor97e44942010-02-04 20:27:47 -0800895
896 /**
Christopher Tate14a7bb02015-10-01 10:24:31 -0700897 * Remove any alarm scheduled to be delivered to the given {@link OnAlarmListener}.
898 *
899 * @param listener OnAlarmListener instance that is the target of a currently-set alarm.
900 */
901 public void cancel(OnAlarmListener listener) {
902 if (listener == null) {
Christopher Tatebb76a6c2015-12-02 10:54:56 -0800903 throw new NullPointerException("cancel() called with a null OnAlarmListener");
Christopher Tate14a7bb02015-10-01 10:24:31 -0700904 }
905
906 ListenerWrapper wrapper = null;
907 synchronized (AlarmManager.class) {
908 final WeakReference<ListenerWrapper> wrapperRef;
909 wrapperRef = sWrappers.get(listener);
910 if (wrapperRef != null) {
911 wrapper = wrapperRef.get();
912 }
913 }
914
915 if (wrapper == null) {
916 Log.w(TAG, "Unrecognized alarm listener " + listener);
917 return;
918 }
919
920 wrapper.cancel();
921 }
922
923 /**
Dan Egnor97e44942010-02-04 20:27:47 -0800924 * Set the system wall clock time.
925 * Requires the permission android.permission.SET_TIME.
926 *
927 * @param millis time in milliseconds since the Epoch
928 */
929 public void setTime(long millis) {
930 try {
931 mService.setTime(millis);
932 } catch (RemoteException ex) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700933 throw ex.rethrowFromSystemServer();
Dan Egnor97e44942010-02-04 20:27:47 -0800934 }
935 }
936
937 /**
Narayan Kamatha78240b2015-04-24 13:22:03 +0100938 * Sets the system's persistent default time zone. This is the time zone for all apps, even
939 * after a reboot. Use {@link java.util.TimeZone#setDefault} if you just want to change the
940 * time zone within your app, and even then prefer to pass an explicit
941 * {@link java.util.TimeZone} to APIs that require it rather than changing the time zone for
942 * all threads.
Dan Egnor97e44942010-02-04 20:27:47 -0800943 *
Narayan Kamatha78240b2015-04-24 13:22:03 +0100944 * <p> On android M and above, it is an error to pass in a non-Olson timezone to this
945 * function. Note that this is a bad idea on all Android releases because POSIX and
946 * the {@code TimeZone} class have opposite interpretations of {@code '+'} and {@code '-'}
947 * in the same non-Olson ID.
948 *
949 * @param timeZone one of the Olson ids from the list returned by
950 * {@link java.util.TimeZone#getAvailableIDs}
Dan Egnor97e44942010-02-04 20:27:47 -0800951 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800952 public void setTimeZone(String timeZone) {
Narayan Kamatha78240b2015-04-24 13:22:03 +0100953 if (TextUtils.isEmpty(timeZone)) {
954 return;
955 }
956
957 // Reject this timezone if it isn't an Olson zone we recognize.
Dianne Hackborn0e3de6c2015-07-29 15:20:21 -0700958 if (mTargetSdkVersion >= Build.VERSION_CODES.M) {
Narayan Kamatha78240b2015-04-24 13:22:03 +0100959 boolean hasTimeZone = false;
960 try {
961 hasTimeZone = ZoneInfoDB.getInstance().hasTimeZone(timeZone);
962 } catch (IOException ignored) {
963 }
964
965 if (!hasTimeZone) {
966 throw new IllegalArgumentException("Timezone: " + timeZone + " is not an Olson ID");
967 }
968 }
969
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800970 try {
971 mService.setTimeZone(timeZone);
972 } catch (RemoteException ex) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700973 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800974 }
975 }
Adrian Roosc42a1e12014-07-07 23:35:53 +0200976
Dianne Hackbornf70faed2015-04-21 14:11:38 -0700977 /** @hide */
978 public long getNextWakeFromIdleTime() {
979 try {
980 return mService.getNextWakeFromIdleTime();
981 } catch (RemoteException ex) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700982 throw ex.rethrowFromSystemServer();
Dianne Hackbornf70faed2015-04-21 14:11:38 -0700983 }
984 }
985
Adrian Roosc42a1e12014-07-07 23:35:53 +0200986 /**
987 * Gets information about the next alarm clock currently scheduled.
988 *
Christopher Tate2affae92016-03-09 17:42:52 -0800989 * The alarm clocks considered are those scheduled by any application
990 * using the {@link #setAlarmClock} method.
991 *
992 * @return An {@link AlarmClockInfo} object describing the next upcoming alarm
993 * clock event that will occur. If there are no alarm clock events currently
994 * scheduled, this method will return {@code null}.
Adrian Roosc42a1e12014-07-07 23:35:53 +0200995 *
996 * @see #setAlarmClock
997 * @see AlarmClockInfo
Christopher Tate2affae92016-03-09 17:42:52 -0800998 * @see #ACTION_NEXT_ALARM_CLOCK_CHANGED
Adrian Roosc42a1e12014-07-07 23:35:53 +0200999 */
1000 public AlarmClockInfo getNextAlarmClock() {
1001 return getNextAlarmClock(UserHandle.myUserId());
1002 }
1003
1004 /**
1005 * Gets information about the next alarm clock currently scheduled.
1006 *
Christopher Tate2affae92016-03-09 17:42:52 -08001007 * The alarm clocks considered are those scheduled by any application
1008 * using the {@link #setAlarmClock} method within the given user.
1009 *
1010 * @return An {@link AlarmClockInfo} object describing the next upcoming alarm
1011 * clock event that will occur within the given user. If there are no alarm clock
1012 * events currently scheduled in that user, this method will return {@code null}.
Adrian Roosc42a1e12014-07-07 23:35:53 +02001013 *
1014 * @see #setAlarmClock
1015 * @see AlarmClockInfo
Christopher Tate2affae92016-03-09 17:42:52 -08001016 * @see #ACTION_NEXT_ALARM_CLOCK_CHANGED
Adrian Roosc42a1e12014-07-07 23:35:53 +02001017 *
1018 * @hide
1019 */
1020 public AlarmClockInfo getNextAlarmClock(int userId) {
1021 try {
1022 return mService.getNextAlarmClock(userId);
1023 } catch (RemoteException ex) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001024 throw ex.rethrowFromSystemServer();
Adrian Roosc42a1e12014-07-07 23:35:53 +02001025 }
1026 }
Jose Lima235510e2014-08-13 12:50:01 -07001027
1028 /**
Christopher Tate2affae92016-03-09 17:42:52 -08001029 * An immutable description of a scheduled "alarm clock" event.
Jose Lima235510e2014-08-13 12:50:01 -07001030 *
1031 * @see AlarmManager#setAlarmClock
1032 * @see AlarmManager#getNextAlarmClock
1033 */
1034 public static final class AlarmClockInfo implements Parcelable {
1035
1036 private final long mTriggerTime;
1037 private final PendingIntent mShowIntent;
1038
1039 /**
1040 * Creates a new alarm clock description.
1041 *
1042 * @param triggerTime time at which the underlying alarm is triggered in wall time
1043 * milliseconds since the epoch
1044 * @param showIntent an intent that can be used to show or edit details of
1045 * the alarm clock.
1046 */
1047 public AlarmClockInfo(long triggerTime, PendingIntent showIntent) {
1048 mTriggerTime = triggerTime;
1049 mShowIntent = showIntent;
1050 }
1051
1052 /**
1053 * Use the {@link #CREATOR}
1054 * @hide
1055 */
1056 AlarmClockInfo(Parcel in) {
1057 mTriggerTime = in.readLong();
1058 mShowIntent = in.readParcelable(PendingIntent.class.getClassLoader());
1059 }
1060
1061 /**
1062 * Returns the time at which the alarm is going to trigger.
1063 *
1064 * This value is UTC wall clock time in milliseconds, as returned by
1065 * {@link System#currentTimeMillis()} for example.
1066 */
1067 public long getTriggerTime() {
1068 return mTriggerTime;
1069 }
1070
1071 /**
Shuhrat Dehkanov66729ff2015-05-13 17:16:29 +09001072 * Returns an intent that can be used to show or edit details of the alarm clock in
Jose Lima235510e2014-08-13 12:50:01 -07001073 * the application that scheduled it.
1074 *
1075 * <p class="note">Beware that any application can retrieve and send this intent,
1076 * potentially with additional fields filled in. See
1077 * {@link PendingIntent#send(android.content.Context, int, android.content.Intent)
1078 * PendingIntent.send()} and {@link android.content.Intent#fillIn Intent.fillIn()}
1079 * for details.
1080 */
1081 public PendingIntent getShowIntent() {
1082 return mShowIntent;
1083 }
1084
1085 @Override
1086 public int describeContents() {
1087 return 0;
1088 }
1089
1090 @Override
1091 public void writeToParcel(Parcel dest, int flags) {
1092 dest.writeLong(mTriggerTime);
1093 dest.writeParcelable(mShowIntent, flags);
1094 }
1095
1096 public static final Creator<AlarmClockInfo> CREATOR = new Creator<AlarmClockInfo>() {
1097 @Override
1098 public AlarmClockInfo createFromParcel(Parcel in) {
1099 return new AlarmClockInfo(in);
1100 }
1101
1102 @Override
1103 public AlarmClockInfo[] newArray(int size) {
1104 return new AlarmClockInfo[size];
1105 }
1106 };
1107 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001108}