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