The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
| 17 | package android.app; |
| 18 | |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 19 | import android.content.Context; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | import android.content.Intent; |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 21 | import android.os.Build; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | import android.os.RemoteException; |
David Christie | ebe51fc | 2013-07-26 13:23:29 -0700 | [diff] [blame] | 23 | import android.os.WorkSource; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | |
| 25 | /** |
| 26 | * This class provides access to the system alarm services. These allow you |
| 27 | * to schedule your application to be run at some point in the future. When |
| 28 | * an alarm goes off, the {@link Intent} that had been registered for it |
| 29 | * is broadcast by the system, automatically starting the target application |
| 30 | * if it is not already running. Registered alarms are retained while the |
| 31 | * device is asleep (and can optionally wake the device up if they go off |
| 32 | * during that time), but will be cleared if it is turned off and rebooted. |
Chris Tate | a34df8a2 | 2009-04-02 23:15:58 -0700 | [diff] [blame] | 33 | * |
| 34 | * <p>The Alarm Manager holds a CPU wake lock as long as the alarm receiver's |
| 35 | * onReceive() method is executing. This guarantees that the phone will not sleep |
| 36 | * until you have finished handling the broadcast. Once onReceive() returns, the |
| 37 | * Alarm Manager releases this wake lock. This means that the phone will in some |
| 38 | * cases sleep as soon as your onReceive() method completes. If your alarm receiver |
| 39 | * called {@link android.content.Context#startService Context.startService()}, it |
| 40 | * is possible that the phone will sleep before the requested service is launched. |
| 41 | * To prevent this, your BroadcastReceiver and Service will need to implement a |
| 42 | * separate wake lock policy to ensure that the phone continues running until the |
| 43 | * service becomes available. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | * |
| 45 | * <p><b>Note: The Alarm Manager is intended for cases where you want to have |
| 46 | * your application code run at a specific time, even if your application is |
| 47 | * not currently running. For normal timing operations (ticks, timeouts, |
| 48 | * etc) it is easier and much more efficient to use |
| 49 | * {@link android.os.Handler}.</b> |
| 50 | * |
| 51 | * <p>You do not |
| 52 | * instantiate this class directly; instead, retrieve it through |
| 53 | * {@link android.content.Context#getSystemService |
| 54 | * Context.getSystemService(Context.ALARM_SERVICE)}. |
| 55 | */ |
| 56 | public class AlarmManager |
| 57 | { |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 58 | private static final String TAG = "AlarmManager"; |
| 59 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | /** |
| 61 | * Alarm time in {@link System#currentTimeMillis System.currentTimeMillis()} |
| 62 | * (wall clock time in UTC), which will wake up the device when |
| 63 | * it goes off. |
| 64 | */ |
| 65 | public static final int RTC_WAKEUP = 0; |
| 66 | /** |
| 67 | * Alarm time in {@link System#currentTimeMillis System.currentTimeMillis()} |
| 68 | * (wall clock time in UTC). This alarm does not wake the |
| 69 | * device up; if it goes off while the device is asleep, it will not be |
| 70 | * delivered until the next time the device wakes up. |
| 71 | */ |
| 72 | public static final int RTC = 1; |
| 73 | /** |
| 74 | * Alarm time in {@link android.os.SystemClock#elapsedRealtime |
| 75 | * SystemClock.elapsedRealtime()} (time since boot, including sleep), |
| 76 | * which will wake up the device when it goes off. |
| 77 | */ |
| 78 | public static final int ELAPSED_REALTIME_WAKEUP = 2; |
| 79 | /** |
| 80 | * Alarm time in {@link android.os.SystemClock#elapsedRealtime |
| 81 | * SystemClock.elapsedRealtime()} (time since boot, including sleep). |
| 82 | * This alarm does not wake the device up; if it goes off while the device |
| 83 | * is asleep, it will not be delivered until the next time the device |
| 84 | * wakes up. |
| 85 | */ |
| 86 | public static final int ELAPSED_REALTIME = 3; |
| 87 | |
Christopher Tate | 57ceaaa | 2013-07-19 16:30:43 -0700 | [diff] [blame] | 88 | /** @hide */ |
| 89 | public static final long WINDOW_EXACT = 0; |
| 90 | /** @hide */ |
| 91 | public static final long WINDOW_HEURISTIC = -1; |
| 92 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 93 | private final IAlarmManager mService; |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 94 | private final boolean mAlwaysExact; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 95 | |
Christopher Tate | 57ceaaa | 2013-07-19 16:30:43 -0700 | [diff] [blame] | 96 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 97 | /** |
| 98 | * package private on purpose |
| 99 | */ |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 100 | AlarmManager(IAlarmManager service, Context ctx) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 101 | mService = service; |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 102 | |
| 103 | final int sdkVersion = ctx.getApplicationInfo().targetSdkVersion; |
Chet Haase | e8222dd | 2013-09-05 07:44:18 -0700 | [diff] [blame] | 104 | mAlwaysExact = (sdkVersion < Build.VERSION_CODES.KITKAT); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 105 | } |
Christopher Tate | 57ceaaa | 2013-07-19 16:30:43 -0700 | [diff] [blame] | 106 | |
| 107 | private long legacyExactLength() { |
| 108 | return (mAlwaysExact ? WINDOW_EXACT : WINDOW_HEURISTIC); |
| 109 | } |
| 110 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 111 | /** |
Christopher Tate | 57ceaaa | 2013-07-19 16:30:43 -0700 | [diff] [blame] | 112 | * <p>Schedule an alarm. <b>Note: for timing operations (ticks, timeouts, |
Christopher Tate | 062bce7 | 2013-10-25 13:59:44 -0700 | [diff] [blame^] | 113 | * etc) it is easier and much more efficient to use {@link android.os.Handler}.</b> |
| 114 | * If there is already an alarm scheduled for the same IntentSender, that previous |
| 115 | * alarm will first be canceled. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 116 | * |
Christopher Tate | 062bce7 | 2013-10-25 13:59:44 -0700 | [diff] [blame^] | 117 | * <p>If the stated trigger time is in the past, the alarm will be triggered |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 118 | * immediately. If there is already an alarm for this Intent |
| 119 | * scheduled (with the equality of two intents being defined by |
| 120 | * {@link Intent#filterEquals}), then it will be removed and replaced by |
| 121 | * this one. |
| 122 | * |
| 123 | * <p> |
Christopher Tate | 062bce7 | 2013-10-25 13:59:44 -0700 | [diff] [blame^] | 124 | * The alarm is an Intent broadcast that goes to a broadcast receiver that |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 125 | * you registered with {@link android.content.Context#registerReceiver} |
| 126 | * or through the <receiver> tag in an AndroidManifest.xml file. |
| 127 | * |
| 128 | * <p> |
| 129 | * Alarm intents are delivered with a data extra of type int called |
| 130 | * {@link Intent#EXTRA_ALARM_COUNT Intent.EXTRA_ALARM_COUNT} that indicates |
| 131 | * how many past alarm events have been accumulated into this intent |
| 132 | * broadcast. Recurring alarms that have gone undelivered because the |
| 133 | * phone was asleep may have a count greater than one when delivered. |
Christopher Tate | 062bce7 | 2013-10-25 13:59:44 -0700 | [diff] [blame^] | 134 | * |
| 135 | * <p> |
| 136 | * <b>Note:</b> Beginning in API 19, the trigger time passed to this method |
| 137 | * is treated as inexact: the alarm will not be delivered before this time, but |
| 138 | * may be deferred and delivered some time later. The OS will use |
| 139 | * this policy in order to "batch" alarms together across the entire system, |
| 140 | * minimizing the number of times the device needs to "wake up" and minimizing |
| 141 | * battery use. In general, alarms scheduled in the near future will not |
| 142 | * be deferred as long as alarms scheduled far in the future. |
| 143 | * |
| 144 | * <p> |
| 145 | * With the new batching policy, delivery ordering guarantees are not as |
| 146 | * strong as they were previously. If the application sets multiple alarms, |
| 147 | * it is possible that these alarms' <i>actual</i> delivery ordering may not match |
| 148 | * the order of their <i>requested</i> delivery times. If your application has |
| 149 | * strong ordering requirements there are other APIs that you can use to get |
| 150 | * the necessary behavior; see {@link #setWindow(int, long, long, PendingIntent)} |
| 151 | * and {@link #setExact(int, long, PendingIntent)}. |
| 152 | * |
| 153 | * <p> |
| 154 | * <b>Note:</b> Applications whose targetSdkVersion is before API 19 will |
| 155 | * continue to get the previous alarm behavior: all of their scheduled alarms |
| 156 | * will be treated as exact. |
| 157 | * |
| 158 | * @param type One of {@link #ELAPSED_REALTIME}, {@link #ELAPSED_REALTIME_WAKEUP}, |
| 159 | * {@link #RTC}, or {@link #RTC_WAKEUP}. |
Jesse Wilson | 79074cd | 2011-12-22 22:51:37 -0500 | [diff] [blame] | 160 | * @param triggerAtMillis time in milliseconds that the alarm should go |
| 161 | * off, using the appropriate clock (depending on the alarm type). |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 162 | * @param operation Action to perform when the alarm goes off; |
| 163 | * typically comes from {@link PendingIntent#getBroadcast |
| 164 | * IntentSender.getBroadcast()}. |
| 165 | * |
| 166 | * @see android.os.Handler |
Christopher Tate | 57ceaaa | 2013-07-19 16:30:43 -0700 | [diff] [blame] | 167 | * @see #setExact |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 168 | * @see #setRepeating |
Christopher Tate | 57ceaaa | 2013-07-19 16:30:43 -0700 | [diff] [blame] | 169 | * @see #setWindow |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 170 | * @see #cancel |
| 171 | * @see android.content.Context#sendBroadcast |
| 172 | * @see android.content.Context#registerReceiver |
| 173 | * @see android.content.Intent#filterEquals |
| 174 | * @see #ELAPSED_REALTIME |
| 175 | * @see #ELAPSED_REALTIME_WAKEUP |
| 176 | * @see #RTC |
| 177 | * @see #RTC_WAKEUP |
| 178 | */ |
Jesse Wilson | 79074cd | 2011-12-22 22:51:37 -0500 | [diff] [blame] | 179 | public void set(int type, long triggerAtMillis, PendingIntent operation) { |
David Christie | ebe51fc | 2013-07-26 13:23:29 -0700 | [diff] [blame] | 180 | setImpl(type, triggerAtMillis, legacyExactLength(), 0, operation, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Schedule a repeating alarm. <b>Note: for timing operations (ticks, |
| 185 | * timeouts, etc) it is easier and much more efficient to use |
| 186 | * {@link android.os.Handler}.</b> If there is already an alarm scheduled |
| 187 | * for the same IntentSender, it will first be canceled. |
| 188 | * |
Christopher Tate | 062bce7 | 2013-10-25 13:59:44 -0700 | [diff] [blame^] | 189 | * <p>Like {@link #set}, except you can also supply a period at which |
| 190 | * the alarm will automatically repeat. This alarm continues |
| 191 | * repeating until explicitly removed with {@link #cancel}. If the stated |
| 192 | * trigger time is in the past, the alarm will be triggered immediately, with an |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 193 | * alarm count depending on how far in the past the trigger time is relative |
| 194 | * to the repeat interval. |
| 195 | * |
| 196 | * <p>If an alarm is delayed (by system sleep, for example, for non |
| 197 | * _WAKEUP alarm types), a skipped repeat will be delivered as soon as |
| 198 | * possible. After that, future alarms will be delivered according to the |
| 199 | * original schedule; they do not drift over time. For example, if you have |
| 200 | * set a recurring alarm for the top of every hour but the phone was asleep |
| 201 | * from 7:45 until 8:45, an alarm will be sent as soon as the phone awakens, |
| 202 | * then the next alarm will be sent at 9:00. |
| 203 | * |
| 204 | * <p>If your application wants to allow the delivery times to drift in |
| 205 | * order to guarantee that at least a certain time interval always elapses |
| 206 | * between alarms, then the approach to take is to use one-time alarms, |
| 207 | * scheduling the next one yourself when handling each alarm delivery. |
| 208 | * |
Christopher Tate | 062bce7 | 2013-10-25 13:59:44 -0700 | [diff] [blame^] | 209 | * <p> |
| 210 | * <b>Note:</b> as of API 19, all repeating alarms are inexact. If your |
| 211 | * application needs precise delivery times then it must use one-time |
| 212 | * exact alarms, rescheduling each time as described above. Legacy applications |
| 213 | * whose targetSdkVersion is earlier than API 19 will continue to have all |
| 214 | * of their alarms, including repeating alarms, treated as exact. |
| 215 | * |
| 216 | * @param type One of {@link #ELAPSED_REALTIME}, {@link #ELAPSED_REALTIME_WAKEUP}, |
| 217 | * {@link #RTC}, or {@link #RTC_WAKEUP}. |
Jesse Wilson | 79074cd | 2011-12-22 22:51:37 -0500 | [diff] [blame] | 218 | * @param triggerAtMillis time in milliseconds that the alarm should first |
| 219 | * go off, using the appropriate clock (depending on the alarm type). |
| 220 | * @param intervalMillis interval in milliseconds between subsequent repeats |
| 221 | * of the alarm. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 222 | * @param operation Action to perform when the alarm goes off; |
| 223 | * typically comes from {@link PendingIntent#getBroadcast |
| 224 | * IntentSender.getBroadcast()}. |
| 225 | * |
| 226 | * @see android.os.Handler |
| 227 | * @see #set |
Christopher Tate | 57ceaaa | 2013-07-19 16:30:43 -0700 | [diff] [blame] | 228 | * @see #setExact |
| 229 | * @see #setWindow |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 230 | * @see #cancel |
| 231 | * @see android.content.Context#sendBroadcast |
| 232 | * @see android.content.Context#registerReceiver |
| 233 | * @see android.content.Intent#filterEquals |
| 234 | * @see #ELAPSED_REALTIME |
| 235 | * @see #ELAPSED_REALTIME_WAKEUP |
| 236 | * @see #RTC |
| 237 | * @see #RTC_WAKEUP |
| 238 | */ |
Jesse Wilson | 79074cd | 2011-12-22 22:51:37 -0500 | [diff] [blame] | 239 | public void setRepeating(int type, long triggerAtMillis, |
| 240 | long intervalMillis, PendingIntent operation) { |
David Christie | ebe51fc | 2013-07-26 13:23:29 -0700 | [diff] [blame] | 241 | setImpl(type, triggerAtMillis, legacyExactLength(), intervalMillis, operation, null); |
Christopher Tate | 57ceaaa | 2013-07-19 16:30:43 -0700 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | /** |
Christopher Tate | 062bce7 | 2013-10-25 13:59:44 -0700 | [diff] [blame^] | 245 | * Schedule an alarm to be delivered within a given window of time. This method |
| 246 | * is similar to {@link #set(int, long, PendingIntent)}, but allows the |
| 247 | * application to precisely control the degree to which its delivery might be |
| 248 | * adjusted by the OS. This method allows an application to take advantage of the |
| 249 | * battery optimizations that arise from delivery batching even when it has |
| 250 | * modest timeliness requirements for its alarms. |
Christopher Tate | 57ceaaa | 2013-07-19 16:30:43 -0700 | [diff] [blame] | 251 | * |
Christopher Tate | 062bce7 | 2013-10-25 13:59:44 -0700 | [diff] [blame^] | 252 | * <p> |
| 253 | * This method can also be used to achieve strict ordering guarantees by ensuring |
| 254 | * that the windows requested for each alarm do not intersect. |
Christopher Tate | 57ceaaa | 2013-07-19 16:30:43 -0700 | [diff] [blame] | 255 | * |
Christopher Tate | 062bce7 | 2013-10-25 13:59:44 -0700 | [diff] [blame^] | 256 | * <p> |
| 257 | * When precise delivery is not required, applications should use the standard |
| 258 | * {@link #set(int, long, PendingIntent)} method. This will give the OS the most |
| 259 | * ability to minimize wakeups and battery use. For alarms that must be delivered |
| 260 | * at precisely-specified times with no acceptable variation, applications can use |
| 261 | * {@link #setExact(int, long, PendingIntent)}. |
| 262 | * |
| 263 | * @param type One of {@link #ELAPSED_REALTIME}, {@link #ELAPSED_REALTIME_WAKEUP}, |
| 264 | * {@link #RTC}, or {@link #RTC_WAKEUP |
Christopher Tate | 57ceaaa | 2013-07-19 16:30:43 -0700 | [diff] [blame] | 265 | * @param windowStartMillis The earliest time, in milliseconds, that the alarm should |
| 266 | * be delivered, expressed in the appropriate clock's units (depending on the alarm |
| 267 | * type). |
| 268 | * @param windowLengthMillis The length of the requested delivery window, |
| 269 | * in milliseconds. The alarm will be delivered no later than this many |
Christopher Tate | 062bce7 | 2013-10-25 13:59:44 -0700 | [diff] [blame^] | 270 | * milliseconds after {@code windowStartMillis}. Note that this parameter |
Christopher Tate | 57ceaaa | 2013-07-19 16:30:43 -0700 | [diff] [blame] | 271 | * is a <i>duration,</i> not the timestamp of the end of the window. |
| 272 | * @param operation Action to perform when the alarm goes off; |
| 273 | * typically comes from {@link PendingIntent#getBroadcast |
| 274 | * IntentSender.getBroadcast()}. |
| 275 | * |
| 276 | * @see #set |
| 277 | * @see #setExact |
| 278 | * @see #setRepeating |
| 279 | * @see #cancel |
| 280 | * @see android.content.Context#sendBroadcast |
| 281 | * @see android.content.Context#registerReceiver |
| 282 | * @see android.content.Intent#filterEquals |
| 283 | * @see #ELAPSED_REALTIME |
| 284 | * @see #ELAPSED_REALTIME_WAKEUP |
| 285 | * @see #RTC |
| 286 | * @see #RTC_WAKEUP |
| 287 | */ |
| 288 | public void setWindow(int type, long windowStartMillis, long windowLengthMillis, |
| 289 | PendingIntent operation) { |
David Christie | ebe51fc | 2013-07-26 13:23:29 -0700 | [diff] [blame] | 290 | setImpl(type, windowStartMillis, windowLengthMillis, 0, operation, null); |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | /** |
Christopher Tate | 062bce7 | 2013-10-25 13:59:44 -0700 | [diff] [blame^] | 294 | * Schedule an alarm to be delivered precisely at the stated time. |
| 295 | * |
| 296 | * <p> |
| 297 | * This method is like {@link #set(int, long, PendingIntent)}, but does not permit |
| 298 | * the OS to adjust the delivery time. The alarm will be delivered as nearly as |
| 299 | * possible to the requested trigger time. |
| 300 | * |
| 301 | * <p> |
| 302 | * <b>Note:</b> only alarms for which there is a strong demand for exact-time |
| 303 | * delivery (such as an alarm clock ringing at the requested time) should be |
| 304 | * scheduled as exact. Applications are strongly discouraged from using exact |
| 305 | * alarms unnecessarily as they reduce the OS's ability to minimize battery use. |
| 306 | * |
| 307 | * @param type One of {@link #ELAPSED_REALTIME}, {@link #ELAPSED_REALTIME_WAKEUP}, |
| 308 | * {@link #RTC}, or {@link #RTC_WAKEUP}. |
| 309 | * @param triggerAtMillis time in milliseconds that the alarm should go |
| 310 | * off, using the appropriate clock (depending on the alarm type). |
| 311 | * @param operation Action to perform when the alarm goes off; |
| 312 | * typically comes from {@link PendingIntent#getBroadcast |
| 313 | * IntentSender.getBroadcast()}. |
| 314 | * |
| 315 | * @see #set |
| 316 | * @see #setRepeating |
| 317 | * @see #setWindow |
| 318 | * @see #cancel |
| 319 | * @see android.content.Context#sendBroadcast |
| 320 | * @see android.content.Context#registerReceiver |
| 321 | * @see android.content.Intent#filterEquals |
| 322 | * @see #ELAPSED_REALTIME |
| 323 | * @see #ELAPSED_REALTIME_WAKEUP |
| 324 | * @see #RTC |
| 325 | * @see #RTC_WAKEUP |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 326 | */ |
| 327 | public void setExact(int type, long triggerAtMillis, PendingIntent operation) { |
David Christie | ebe51fc | 2013-07-26 13:23:29 -0700 | [diff] [blame] | 328 | setImpl(type, triggerAtMillis, WINDOW_EXACT, 0, operation, null); |
| 329 | } |
| 330 | |
| 331 | /** @hide */ |
| 332 | public void set(int type, long triggerAtMillis, long windowMillis, long intervalMillis, |
| 333 | PendingIntent operation, WorkSource workSource) { |
| 334 | setImpl(type, triggerAtMillis, windowMillis, intervalMillis, operation, workSource); |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 335 | } |
| 336 | |
Christopher Tate | 57ceaaa | 2013-07-19 16:30:43 -0700 | [diff] [blame] | 337 | private void setImpl(int type, long triggerAtMillis, long windowMillis, long intervalMillis, |
David Christie | ebe51fc | 2013-07-26 13:23:29 -0700 | [diff] [blame] | 338 | PendingIntent operation, WorkSource workSource) { |
Christopher Tate | 5f221e8 | 2013-07-30 17:13:15 -0700 | [diff] [blame] | 339 | if (triggerAtMillis < 0) { |
Christopher Tate | 56cfa24 | 2013-07-30 19:09:41 -0700 | [diff] [blame] | 340 | /* NOTYET |
Christopher Tate | 5f221e8 | 2013-07-30 17:13:15 -0700 | [diff] [blame] | 341 | if (mAlwaysExact) { |
| 342 | // Fatal error for KLP+ apps to use negative trigger times |
| 343 | throw new IllegalArgumentException("Invalid alarm trigger time " |
| 344 | + triggerAtMillis); |
| 345 | } |
Christopher Tate | 56cfa24 | 2013-07-30 19:09:41 -0700 | [diff] [blame] | 346 | */ |
Christopher Tate | 5f221e8 | 2013-07-30 17:13:15 -0700 | [diff] [blame] | 347 | triggerAtMillis = 0; |
| 348 | } |
David Christie | ebe51fc | 2013-07-26 13:23:29 -0700 | [diff] [blame] | 349 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 350 | try { |
David Christie | ebe51fc | 2013-07-26 13:23:29 -0700 | [diff] [blame] | 351 | mService.set(type, triggerAtMillis, windowMillis, intervalMillis, operation, |
| 352 | workSource); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 353 | } catch (RemoteException ex) { |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | /** |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 358 | * @deprecated setInexactRepeating() is deprecated; as of API 19 all |
| 359 | * repeating alarms are inexact. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 360 | */ |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 361 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 362 | public static final long INTERVAL_FIFTEEN_MINUTES = 15 * 60 * 1000; |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 363 | |
| 364 | /** |
| 365 | * @deprecated setInexactRepeating() is deprecated; as of API 19 all |
| 366 | * repeating alarms are inexact. |
| 367 | */ |
| 368 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 369 | public static final long INTERVAL_HALF_HOUR = 2*INTERVAL_FIFTEEN_MINUTES; |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 370 | |
| 371 | /** |
| 372 | * @deprecated setInexactRepeating() is deprecated; as of API 19 all |
| 373 | * repeating alarms are inexact. |
| 374 | */ |
| 375 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 376 | public static final long INTERVAL_HOUR = 2*INTERVAL_HALF_HOUR; |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 377 | |
| 378 | /** |
| 379 | * @deprecated setInexactRepeating() is deprecated; as of API 19 all |
| 380 | * repeating alarms are inexact. |
| 381 | */ |
| 382 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 383 | public static final long INTERVAL_HALF_DAY = 12*INTERVAL_HOUR; |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 384 | |
| 385 | /** |
| 386 | * @deprecated setInexactRepeating() is deprecated; as of API 19 all |
| 387 | * repeating alarms are inexact. |
| 388 | */ |
| 389 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 390 | public static final long INTERVAL_DAY = 2*INTERVAL_HALF_DAY; |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 391 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 392 | /** |
| 393 | * Schedule a repeating alarm that has inexact trigger time requirements; |
| 394 | * for example, an alarm that repeats every hour, but not necessarily at |
| 395 | * the top of every hour. These alarms are more power-efficient than |
| 396 | * the strict recurrences supplied by {@link #setRepeating}, since the |
| 397 | * system can adjust alarms' phase to cause them to fire simultaneously, |
| 398 | * avoiding waking the device from sleep more than necessary. |
| 399 | * |
| 400 | * <p>Your alarm's first trigger will not be before the requested time, |
| 401 | * but it might not occur for almost a full interval after that time. In |
| 402 | * addition, while the overall period of the repeating alarm will be as |
| 403 | * requested, the time between any two successive firings of the alarm |
| 404 | * may vary. If your application demands very low jitter, use |
| 405 | * {@link #setRepeating} instead. |
Jesse Wilson | 79074cd | 2011-12-22 22:51:37 -0500 | [diff] [blame] | 406 | * |
Christopher Tate | 062bce7 | 2013-10-25 13:59:44 -0700 | [diff] [blame^] | 407 | * @param type One of {@link #ELAPSED_REALTIME}, {@link #ELAPSED_REALTIME_WAKEUP}, |
| 408 | * {@link #RTC}, or {@link #RTC_WAKEUP}. |
Jesse Wilson | 79074cd | 2011-12-22 22:51:37 -0500 | [diff] [blame] | 409 | * @param triggerAtMillis time in milliseconds that the alarm should first |
| 410 | * go off, using the appropriate clock (depending on the alarm type). This |
| 411 | * is inexact: the alarm will not fire before this time, but there may be a |
| 412 | * delay of almost an entire alarm interval before the first invocation of |
| 413 | * the alarm. |
| 414 | * @param intervalMillis interval in milliseconds between subsequent repeats |
| 415 | * of the alarm. If this is one of INTERVAL_FIFTEEN_MINUTES, |
| 416 | * INTERVAL_HALF_HOUR, INTERVAL_HOUR, INTERVAL_HALF_DAY, or INTERVAL_DAY |
| 417 | * then the alarm will be phase-aligned with other alarms to reduce the |
| 418 | * number of wakeups. Otherwise, the alarm will be set as though the |
| 419 | * application had called {@link #setRepeating}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 420 | * @param operation Action to perform when the alarm goes off; |
| 421 | * typically comes from {@link PendingIntent#getBroadcast |
| 422 | * IntentSender.getBroadcast()}. |
| 423 | * |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 424 | * @deprecated As of API 19, all repeating alarms are inexact. |
| 425 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 426 | * @see android.os.Handler |
| 427 | * @see #set |
| 428 | * @see #cancel |
| 429 | * @see android.content.Context#sendBroadcast |
| 430 | * @see android.content.Context#registerReceiver |
| 431 | * @see android.content.Intent#filterEquals |
| 432 | * @see #ELAPSED_REALTIME |
| 433 | * @see #ELAPSED_REALTIME_WAKEUP |
| 434 | * @see #RTC |
| 435 | * @see #RTC_WAKEUP |
| 436 | * @see #INTERVAL_FIFTEEN_MINUTES |
| 437 | * @see #INTERVAL_HALF_HOUR |
| 438 | * @see #INTERVAL_HOUR |
| 439 | * @see #INTERVAL_HALF_DAY |
| 440 | * @see #INTERVAL_DAY |
| 441 | */ |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 442 | @Deprecated |
Jesse Wilson | 79074cd | 2011-12-22 22:51:37 -0500 | [diff] [blame] | 443 | public void setInexactRepeating(int type, long triggerAtMillis, |
| 444 | long intervalMillis, PendingIntent operation) { |
David Christie | ebe51fc | 2013-07-26 13:23:29 -0700 | [diff] [blame] | 445 | setImpl(type, triggerAtMillis, WINDOW_HEURISTIC, intervalMillis, operation, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | /** |
| 449 | * Remove any alarms with a matching {@link Intent}. |
| 450 | * Any alarm, of any type, whose Intent matches this one (as defined by |
| 451 | * {@link Intent#filterEquals}), will be canceled. |
| 452 | * |
| 453 | * @param operation IntentSender which matches a previously added |
| 454 | * IntentSender. |
| 455 | * |
| 456 | * @see #set |
| 457 | */ |
| 458 | public void cancel(PendingIntent operation) { |
| 459 | try { |
| 460 | mService.remove(operation); |
| 461 | } catch (RemoteException ex) { |
| 462 | } |
| 463 | } |
Dan Egnor | 97e4494 | 2010-02-04 20:27:47 -0800 | [diff] [blame] | 464 | |
| 465 | /** |
| 466 | * Set the system wall clock time. |
| 467 | * Requires the permission android.permission.SET_TIME. |
| 468 | * |
| 469 | * @param millis time in milliseconds since the Epoch |
| 470 | */ |
| 471 | public void setTime(long millis) { |
| 472 | try { |
| 473 | mService.setTime(millis); |
| 474 | } catch (RemoteException ex) { |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | /** |
| 479 | * Set the system default time zone. |
| 480 | * Requires the permission android.permission.SET_TIME_ZONE. |
| 481 | * |
| 482 | * @param timeZone in the format understood by {@link java.util.TimeZone} |
| 483 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 484 | public void setTimeZone(String timeZone) { |
| 485 | try { |
| 486 | mService.setTimeZone(timeZone); |
| 487 | } catch (RemoteException ex) { |
| 488 | } |
| 489 | } |
| 490 | } |