1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160
|
/*
* Copyright (C) 2007 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.app;
import android.annotation.IntDef;
import android.annotation.RequiresPermission;
import android.annotation.SdkConstant;
import android.annotation.SystemApi;
import android.annotation.SystemService;
import android.annotation.UnsupportedAppUsage;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Handler;
import android.os.Parcel;
import android.os.Parcelable;
import android.os.RemoteException;
import android.os.WorkSource;
import android.text.TextUtils;
import android.util.ArrayMap;
import android.util.Log;
import android.util.proto.ProtoOutputStream;
import libcore.timezone.ZoneInfoDB;
import java.io.IOException;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
/**
* This class provides access to the system alarm services. These allow you
* to schedule your application to be run at some point in the future. When
* an alarm goes off, the {@link Intent} that had been registered for it
* is broadcast by the system, automatically starting the target application
* if it is not already running. Registered alarms are retained while the
* device is asleep (and can optionally wake the device up if they go off
* during that time), but will be cleared if it is turned off and rebooted.
*
* <p>The Alarm Manager holds a CPU wake lock as long as the alarm receiver's
* onReceive() method is executing. This guarantees that the phone will not sleep
* until you have finished handling the broadcast. Once onReceive() returns, the
* Alarm Manager releases this wake lock. This means that the phone will in some
* cases sleep as soon as your onReceive() method completes. If your alarm receiver
* called {@link android.content.Context#startService Context.startService()}, it
* is possible that the phone will sleep before the requested service is launched.
* To prevent this, your BroadcastReceiver and Service will need to implement a
* separate wake lock policy to ensure that the phone continues running until the
* service becomes available.
*
* <p><b>Note: The Alarm Manager is intended for cases where you want to have
* your application code run at a specific time, even if your application is
* not currently running. For normal timing operations (ticks, timeouts,
* etc) it is easier and much more efficient to use
* {@link android.os.Handler}.</b>
*
* <p class="caution"><strong>Note:</strong> Beginning with API 19
* ({@link android.os.Build.VERSION_CODES#KITKAT}) alarm delivery is inexact:
* the OS will shift alarms in order to minimize wakeups and battery use. There are
* new APIs to support applications which need strict delivery guarantees; see
* {@link #setWindow(int, long, long, PendingIntent)} and
* {@link #setExact(int, long, PendingIntent)}. Applications whose {@code targetSdkVersion}
* is earlier than API 19 will continue to see the previous behavior in which all
* alarms are delivered exactly when requested.
*/
@SystemService(Context.ALARM_SERVICE)
public class AlarmManager {
private static final String TAG = "AlarmManager";
/** @hide */
@IntDef(prefix = { "RTC", "ELAPSED" }, value = {
RTC_WAKEUP,
RTC,
ELAPSED_REALTIME_WAKEUP,
ELAPSED_REALTIME,
})
@Retention(RetentionPolicy.SOURCE)
public @interface AlarmType {}
/**
* Alarm time in {@link System#currentTimeMillis System.currentTimeMillis()}
* (wall clock time in UTC), which will wake up the device when
* it goes off.
*/
public static final int RTC_WAKEUP = 0;
/**
* Alarm time in {@link System#currentTimeMillis System.currentTimeMillis()}
* (wall clock time in UTC). This alarm does not wake the
* device up; if it goes off while the device is asleep, it will not be
* delivered until the next time the device wakes up.
*/
public static final int RTC = 1;
/**
* Alarm time in {@link android.os.SystemClock#elapsedRealtime
* SystemClock.elapsedRealtime()} (time since boot, including sleep),
* which will wake up the device when it goes off.
*/
public static final int ELAPSED_REALTIME_WAKEUP = 2;
/**
* Alarm time in {@link android.os.SystemClock#elapsedRealtime
* SystemClock.elapsedRealtime()} (time since boot, including sleep).
* This alarm does not wake the device up; if it goes off while the device
* is asleep, it will not be delivered until the next time the device
* wakes up.
*/
public static final int ELAPSED_REALTIME = 3;
/**
* Broadcast Action: Sent after the value returned by
* {@link #getNextAlarmClock()} has changed.
*
* <p class="note">This is a protected intent that can only be sent by the system.
* It is only sent to registered receivers.</p>
*/
@SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
public static final String ACTION_NEXT_ALARM_CLOCK_CHANGED =
"android.app.action.NEXT_ALARM_CLOCK_CHANGED";
/** @hide */
@UnsupportedAppUsage
public static final long WINDOW_EXACT = 0;
/** @hide */
@UnsupportedAppUsage
public static final long WINDOW_HEURISTIC = -1;
/**
* Flag for alarms: this is to be a stand-alone alarm, that should not be batched with
* other alarms.
* @hide
*/
@UnsupportedAppUsage
public static final int FLAG_STANDALONE = 1<<0;
/**
* Flag for alarms: this alarm would like to wake the device even if it is idle. This
* is, for example, an alarm for an alarm clock.
* @hide
*/
@UnsupportedAppUsage
public static final int FLAG_WAKE_FROM_IDLE = 1<<1;
/**
* Flag for alarms: this alarm would like to still execute even if the device is
* idle. This won't bring the device out of idle, just allow this specific alarm to
* run. Note that this means the actual time this alarm goes off can be inconsistent
* with the time of non-allow-while-idle alarms (it could go earlier than the time
* requested by another alarm).
*
* @hide
*/
public static final int FLAG_ALLOW_WHILE_IDLE = 1<<2;
/**
* Flag for alarms: same as {@link #FLAG_ALLOW_WHILE_IDLE}, but doesn't have restrictions
* on how frequently it can be scheduled. Only available (and automatically applied) to
* system alarms.
*
* @hide
*/
@UnsupportedAppUsage
public static final int FLAG_ALLOW_WHILE_IDLE_UNRESTRICTED = 1<<3;
/**
* Flag for alarms: this alarm marks the point where we would like to come out of idle
* mode. It may be moved by the alarm manager to match the first wake-from-idle alarm.
* Scheduling an alarm with this flag puts the alarm manager in to idle mode, where it
* avoids scheduling any further alarms until the marker alarm is executed.
* @hide
*/
@UnsupportedAppUsage
public static final int FLAG_IDLE_UNTIL = 1<<4;
@UnsupportedAppUsage
private final IAlarmManager mService;
private final Context mContext;
private final String mPackageName;
private final boolean mAlwaysExact;
private final int mTargetSdkVersion;
private final Handler mMainThreadHandler;
/**
* Direct-notification alarms: the requester must be running continuously from the
* time the alarm is set to the time it is delivered, or delivery will fail. Only
* one-shot alarms can be set using this mechanism, not repeating alarms.
*/
public interface OnAlarmListener {
/**
* Callback method that is invoked by the system when the alarm time is reached.
*/
public void onAlarm();
}
final class ListenerWrapper extends IAlarmListener.Stub implements Runnable {
final OnAlarmListener mListener;
Handler mHandler;
IAlarmCompleteListener mCompletion;
public ListenerWrapper(OnAlarmListener listener) {
mListener = listener;
}
public void setHandler(Handler h) {
mHandler = h;
}
public void cancel() {
try {
mService.remove(null, this);
} catch (RemoteException ex) {
throw ex.rethrowFromSystemServer();
}
synchronized (AlarmManager.class) {
if (sWrappers != null) {
sWrappers.remove(mListener);
}
}
}
@Override
public void doAlarm(IAlarmCompleteListener alarmManager) {
mCompletion = alarmManager;
// Remove this listener from the wrapper cache first; the server side
// already considers it gone
synchronized (AlarmManager.class) {
if (sWrappers != null) {
sWrappers.remove(mListener);
}
}
mHandler.post(this);
}
@Override
public void run() {
// Now deliver it to the app
try {
mListener.onAlarm();
} finally {
// No catch -- make sure to report completion to the system process,
// but continue to allow the exception to crash the app.
try {
mCompletion.alarmComplete(this);
} catch (Exception e) {
Log.e(TAG, "Unable to report completion to Alarm Manager!", e);
}
}
}
}
// Tracking of the OnAlarmListener -> wrapper mapping, for cancel() support.
// Access is synchronized on the AlarmManager class object.
private static ArrayMap<OnAlarmListener, ListenerWrapper> sWrappers;
/**
* package private on purpose
*/
AlarmManager(IAlarmManager service, Context ctx) {
mService = service;
mContext = ctx;
mPackageName = ctx.getPackageName();
mTargetSdkVersion = ctx.getApplicationInfo().targetSdkVersion;
mAlwaysExact = (mTargetSdkVersion < Build.VERSION_CODES.KITKAT);
mMainThreadHandler = new Handler(ctx.getMainLooper());
}
private long legacyExactLength() {
return (mAlwaysExact ? WINDOW_EXACT : WINDOW_HEURISTIC);
}
/**
* <p>Schedule an alarm. <b>Note: for timing operations (ticks, timeouts,
* etc) it is easier and much more efficient to use {@link android.os.Handler}.</b>
* If there is already an alarm scheduled for the same IntentSender, that previous
* alarm will first be canceled.
*
* <p>If the stated trigger time is in the past, the alarm will be triggered
* immediately. If there is already an alarm for this Intent
* scheduled (with the equality of two intents being defined by
* {@link Intent#filterEquals}), then it will be removed and replaced by
* this one.
*
* <p>
* The alarm is an Intent broadcast that goes to a broadcast receiver that
* you registered with {@link android.content.Context#registerReceiver}
* or through the <receiver> tag in an AndroidManifest.xml file.
*
* <p>
* Alarm intents are delivered with a data extra of type int called
* {@link Intent#EXTRA_ALARM_COUNT Intent.EXTRA_ALARM_COUNT} that indicates
* how many past alarm events have been accumulated into this intent
* broadcast. Recurring alarms that have gone undelivered because the
* phone was asleep may have a count greater than one when delivered.
*
* <div class="note">
* <p>
* <b>Note:</b> Beginning in API 19, the trigger time passed to this method
* is treated as inexact: the alarm will not be delivered before this time, but
* may be deferred and delivered some time later. The OS will use
* this policy in order to "batch" alarms together across the entire system,
* minimizing the number of times the device needs to "wake up" and minimizing
* battery use. In general, alarms scheduled in the near future will not
* be deferred as long as alarms scheduled far in the future.
*
* <p>
* With the new batching policy, delivery ordering guarantees are not as
* strong as they were previously. If the application sets multiple alarms,
* it is possible that these alarms' <em>actual</em> delivery ordering may not match
* the order of their <em>requested</em> delivery times. If your application has
* strong ordering requirements there are other APIs that you can use to get
* the necessary behavior; see {@link #setWindow(int, long, long, PendingIntent)}
* and {@link #setExact(int, long, PendingIntent)}.
*
* <p>
* Applications whose {@code targetSdkVersion} is before API 19 will
* continue to get the previous alarm behavior: all of their scheduled alarms
* will be treated as exact.
* </div>
*
* @param type type of alarm.
* @param triggerAtMillis time in milliseconds that the alarm should go
* off, using the appropriate clock (depending on the alarm type).
* @param operation Action to perform when the alarm goes off;
* typically comes from {@link PendingIntent#getBroadcast
* IntentSender.getBroadcast()}.
*
* @see android.os.Handler
* @see #setExact
* @see #setRepeating
* @see #setWindow
* @see #cancel
* @see android.content.Context#sendBroadcast
* @see android.content.Context#registerReceiver
* @see android.content.Intent#filterEquals
* @see #ELAPSED_REALTIME
* @see #ELAPSED_REALTIME_WAKEUP
* @see #RTC
* @see #RTC_WAKEUP
*/
public void set(@AlarmType int type, long triggerAtMillis, PendingIntent operation) {
setImpl(type, triggerAtMillis, legacyExactLength(), 0, 0, operation, null, null,
null, null, null);
}
/**
* Direct callback version of {@link #set(int, long, PendingIntent)}. Rather than
* supplying a PendingIntent to be sent when the alarm time is reached, this variant
* supplies an {@link OnAlarmListener} instance that will be invoked at that time.
* <p>
* The OnAlarmListener's {@link OnAlarmListener#onAlarm() onAlarm()} method will be
* invoked via the specified target Handler, or on the application's main looper
* if {@code null} is passed as the {@code targetHandler} parameter.
*
* @param type type of alarm.
* @param triggerAtMillis time in milliseconds that the alarm should go
* off, using the appropriate clock (depending on the alarm type).
* @param tag string describing the alarm, used for logging and battery-use
* attribution
* @param listener {@link OnAlarmListener} instance whose
* {@link OnAlarmListener#onAlarm() onAlarm()} method will be
* called when the alarm time is reached. A given OnAlarmListener instance can
* only be the target of a single pending alarm, just as a given PendingIntent
* can only be used with one alarm at a time.
* @param targetHandler {@link Handler} on which to execute the listener's onAlarm()
* callback, or {@code null} to run that callback on the main looper.
*/
public void set(@AlarmType int type, long triggerAtMillis, String tag, OnAlarmListener listener,
Handler targetHandler) {
setImpl(type, triggerAtMillis, legacyExactLength(), 0, 0, null, listener, tag,
targetHandler, null, null);
}
/**
* Schedule a repeating alarm. <b>Note: for timing operations (ticks,
* timeouts, etc) it is easier and much more efficient to use
* {@link android.os.Handler}.</b> If there is already an alarm scheduled
* for the same IntentSender, it will first be canceled.
*
* <p>Like {@link #set}, except you can also supply a period at which
* the alarm will automatically repeat. This alarm continues
* repeating until explicitly removed with {@link #cancel}. If the stated
* trigger time is in the past, the alarm will be triggered immediately, with an
* alarm count depending on how far in the past the trigger time is relative
* to the repeat interval.
*
* <p>If an alarm is delayed (by system sleep, for example, for non
* _WAKEUP alarm types), a skipped repeat will be delivered as soon as
* possible. After that, future alarms will be delivered according to the
* original schedule; they do not drift over time. For example, if you have
* set a recurring alarm for the top of every hour but the phone was asleep
* from 7:45 until 8:45, an alarm will be sent as soon as the phone awakens,
* then the next alarm will be sent at 9:00.
*
* <p>If your application wants to allow the delivery times to drift in
* order to guarantee that at least a certain time interval always elapses
* between alarms, then the approach to take is to use one-time alarms,
* scheduling the next one yourself when handling each alarm delivery.
*
* <p class="note">
* <b>Note:</b> as of API 19, all repeating alarms are inexact. If your
* application needs precise delivery times then it must use one-time
* exact alarms, rescheduling each time as described above. Legacy applications
* whose {@code targetSdkVersion} is earlier than API 19 will continue to have all
* of their alarms, including repeating alarms, treated as exact.
*
* @param type type of alarm.
* @param triggerAtMillis time in milliseconds that the alarm should first
* go off, using the appropriate clock (depending on the alarm type).
* @param intervalMillis interval in milliseconds between subsequent repeats
* of the alarm.
* @param operation Action to perform when the alarm goes off;
* typically comes from {@link PendingIntent#getBroadcast
* IntentSender.getBroadcast()}.
*
* @see android.os.Handler
* @see #set
* @see #setExact
* @see #setWindow
* @see #cancel
* @see android.content.Context#sendBroadcast
* @see android.content.Context#registerReceiver
* @see android.content.Intent#filterEquals
* @see #ELAPSED_REALTIME
* @see #ELAPSED_REALTIME_WAKEUP
* @see #RTC
* @see #RTC_WAKEUP
*/
public void setRepeating(@AlarmType int type, long triggerAtMillis,
long intervalMillis, PendingIntent operation) {
setImpl(type, triggerAtMillis, legacyExactLength(), intervalMillis, 0, operation,
null, null, null, null, null);
}
/**
* Schedule an alarm to be delivered within a given window of time. This method
* is similar to {@link #set(int, long, PendingIntent)}, but allows the
* application to precisely control the degree to which its delivery might be
* adjusted by the OS. This method allows an application to take advantage of the
* battery optimizations that arise from delivery batching even when it has
* modest timeliness requirements for its alarms.
*
* <p>
* This method can also be used to achieve strict ordering guarantees among
* multiple alarms by ensuring that the windows requested for each alarm do
* not intersect.
*
* <p>
* When precise delivery is not required, applications should use the standard
* {@link #set(int, long, PendingIntent)} method. This will give the OS the most
* flexibility to minimize wakeups and battery use. For alarms that must be delivered
* at precisely-specified times with no acceptable variation, applications can use
* {@link #setExact(int, long, PendingIntent)}.
*
* @param type type of alarm.
* @param windowStartMillis The earliest time, in milliseconds, that the alarm should
* be delivered, expressed in the appropriate clock's units (depending on the alarm
* type).
* @param windowLengthMillis The length of the requested delivery window,
* in milliseconds. The alarm will be delivered no later than this many
* milliseconds after {@code windowStartMillis}. Note that this parameter
* is a <i>duration,</i> not the timestamp of the end of the window.
* @param operation Action to perform when the alarm goes off;
* typically comes from {@link PendingIntent#getBroadcast
* IntentSender.getBroadcast()}.
*
* @see #set
* @see #setExact
* @see #setRepeating
* @see #cancel
* @see android.content.Context#sendBroadcast
* @see android.content.Context#registerReceiver
* @see android.content.Intent#filterEquals
* @see #ELAPSED_REALTIME
* @see #ELAPSED_REALTIME_WAKEUP
* @see #RTC
* @see #RTC_WAKEUP
*/
public void setWindow(@AlarmType int type, long windowStartMillis, long windowLengthMillis,
PendingIntent operation) {
setImpl(type, windowStartMillis, windowLengthMillis, 0, 0, operation,
null, null, null, null, null);
}
/**
* Direct callback version of {@link #setWindow(int, long, long, PendingIntent)}. Rather
* than supplying a PendingIntent to be sent when the alarm time is reached, this variant
* supplies an {@link OnAlarmListener} instance that will be invoked at that time.
* <p>
* The OnAlarmListener {@link OnAlarmListener#onAlarm() onAlarm()} method will be
* invoked via the specified target Handler, or on the application's main looper
* if {@code null} is passed as the {@code targetHandler} parameter.
*/
public void setWindow(@AlarmType int type, long windowStartMillis, long windowLengthMillis,
String tag, OnAlarmListener listener, Handler targetHandler) {
setImpl(type, windowStartMillis, windowLengthMillis, 0, 0, null, listener, tag,
targetHandler, null, null);
}
/**
* Schedule an alarm to be delivered precisely at the stated time.
*
* <p>
* This method is like {@link #set(int, long, PendingIntent)}, but does not permit
* the OS to adjust the delivery time. The alarm will be delivered as nearly as
* possible to the requested trigger time.
*
* <p>
* <b>Note:</b> only alarms for which there is a strong demand for exact-time
* delivery (such as an alarm clock ringing at the requested time) should be
* scheduled as exact. Applications are strongly discouraged from using exact
* alarms unnecessarily as they reduce the OS's ability to minimize battery use.
*
* @param type type of alarm.
* @param triggerAtMillis time in milliseconds that the alarm should go
* off, using the appropriate clock (depending on the alarm type).
* @param operation Action to perform when the alarm goes off;
* typically comes from {@link PendingIntent#getBroadcast
* IntentSender.getBroadcast()}.
*
* @see #set
* @see #setRepeating
* @see #setWindow
* @see #cancel
* @see android.content.Context#sendBroadcast
* @see android.content.Context#registerReceiver
* @see android.content.Intent#filterEquals
* @see #ELAPSED_REALTIME
* @see #ELAPSED_REALTIME_WAKEUP
* @see #RTC
* @see #RTC_WAKEUP
*/
public void setExact(@AlarmType int type, long triggerAtMillis, PendingIntent operation) {
setImpl(type, triggerAtMillis, WINDOW_EXACT, 0, 0, operation, null, null, null,
null, null);
}
/**
* Direct callback version of {@link #setExact(int, long, PendingIntent)}. Rather
* than supplying a PendingIntent to be sent when the alarm time is reached, this variant
* supplies an {@link OnAlarmListener} instance that will be invoked at that time.
* <p>
* The OnAlarmListener's {@link OnAlarmListener#onAlarm() onAlarm()} method will be
* invoked via the specified target Handler, or on the application's main looper
* if {@code null} is passed as the {@code targetHandler} parameter.
*/
public void setExact(@AlarmType int type, long triggerAtMillis, String tag,
OnAlarmListener listener, Handler targetHandler) {
setImpl(type, triggerAtMillis, WINDOW_EXACT, 0, 0, null, listener, tag,
targetHandler, null, null);
}
/**
* Schedule an idle-until alarm, which will keep the alarm manager idle until
* the given time.
* @hide
*/
public void setIdleUntil(@AlarmType int type, long triggerAtMillis, String tag,
OnAlarmListener listener, Handler targetHandler) {
setImpl(type, triggerAtMillis, WINDOW_EXACT, 0, FLAG_IDLE_UNTIL, null,
listener, tag, targetHandler, null, null);
}
/**
* Schedule an alarm that represents an alarm clock, which will be used to notify the user
* when it goes off. The expectation is that when this alarm triggers, the application will
* further wake up the device to tell the user about the alarm -- turning on the screen,
* playing a sound, vibrating, etc. As such, the system will typically also use the
* information supplied here to tell the user about this upcoming alarm if appropriate.
*
* <p>Due to the nature of this kind of alarm, similar to {@link #setExactAndAllowWhileIdle},
* these alarms will be allowed to trigger even if the system is in a low-power idle
* (a.k.a. doze) mode. The system may also do some prep-work when it sees that such an
* alarm coming up, to reduce the amount of background work that could happen if this
* causes the device to fully wake up -- this is to avoid situations such as a large number
* of devices having an alarm set at the same time in the morning, all waking up at that
* time and suddenly swamping the network with pending background work. As such, these
* types of alarms can be extremely expensive on battery use and should only be used for
* their intended purpose.</p>
*
* <p>
* This method is like {@link #setExact(int, long, PendingIntent)}, but implies
* {@link #RTC_WAKEUP}.
*
* @param info
* @param operation Action to perform when the alarm goes off;
* typically comes from {@link PendingIntent#getBroadcast
* IntentSender.getBroadcast()}.
*
* @see #set
* @see #setRepeating
* @see #setWindow
* @see #setExact
* @see #cancel
* @see #getNextAlarmClock()
* @see android.content.Context#sendBroadcast
* @see android.content.Context#registerReceiver
* @see android.content.Intent#filterEquals
*/
public void setAlarmClock(AlarmClockInfo info, PendingIntent operation) {
setImpl(RTC_WAKEUP, info.getTriggerTime(), WINDOW_EXACT, 0, 0, operation,
null, null, null, null, info);
}
/** @hide */
@SystemApi
@RequiresPermission(android.Manifest.permission.UPDATE_DEVICE_STATS)
public void set(@AlarmType int type, long triggerAtMillis, long windowMillis,
long intervalMillis, PendingIntent operation, WorkSource workSource) {
setImpl(type, triggerAtMillis, windowMillis, intervalMillis, 0, operation, null, null,
null, workSource, null);
}
/**
* Direct callback version of {@link #set(int, long, long, long, PendingIntent, WorkSource)}.
* Note that repeating alarms must use the PendingIntent variant, not an OnAlarmListener.
* <p>
* The OnAlarmListener's {@link OnAlarmListener#onAlarm() onAlarm()} method will be
* invoked via the specified target Handler, or on the application's main looper
* if {@code null} is passed as the {@code targetHandler} parameter.
*
* @hide
*/
@UnsupportedAppUsage
public void set(@AlarmType int type, long triggerAtMillis, long windowMillis,
long intervalMillis, String tag, OnAlarmListener listener, Handler targetHandler,
WorkSource workSource) {
setImpl(type, triggerAtMillis, windowMillis, intervalMillis, 0, null, listener, tag,
targetHandler, workSource, null);
}
/**
* Direct callback version of {@link #set(int, long, long, long, PendingIntent, WorkSource)}.
* Note that repeating alarms must use the PendingIntent variant, not an OnAlarmListener.
* <p>
* The OnAlarmListener's {@link OnAlarmListener#onAlarm() onAlarm()} method will be
* invoked via the specified target Handler, or on the application's main looper
* if {@code null} is passed as the {@code targetHandler} parameter.
*
* @hide
*/
@SystemApi
@RequiresPermission(android.Manifest.permission.UPDATE_DEVICE_STATS)
public void set(@AlarmType int type, long triggerAtMillis, long windowMillis,
long intervalMillis, OnAlarmListener listener, Handler targetHandler,
WorkSource workSource) {
setImpl(type, triggerAtMillis, windowMillis, intervalMillis, 0, null, listener, null,
targetHandler, workSource, null);
}
private void setImpl(@AlarmType int type, long triggerAtMillis, long windowMillis,
long intervalMillis, int flags, PendingIntent operation, final OnAlarmListener listener,
String listenerTag, Handler targetHandler, WorkSource workSource,
AlarmClockInfo alarmClock) {
if (triggerAtMillis < 0) {
/* NOTYET
if (mAlwaysExact) {
// Fatal error for KLP+ apps to use negative trigger times
throw new IllegalArgumentException("Invalid alarm trigger time "
+ triggerAtMillis);
}
*/
triggerAtMillis = 0;
}
ListenerWrapper recipientWrapper = null;
if (listener != null) {
synchronized (AlarmManager.class) {
if (sWrappers == null) {
sWrappers = new ArrayMap<OnAlarmListener, ListenerWrapper>();
}
recipientWrapper = sWrappers.get(listener);
// no existing wrapper => build a new one
if (recipientWrapper == null) {
recipientWrapper = new ListenerWrapper(listener);
sWrappers.put(listener, recipientWrapper);
}
}
final Handler handler = (targetHandler != null) ? targetHandler : mMainThreadHandler;
recipientWrapper.setHandler(handler);
}
try {
mService.set(mPackageName, type, triggerAtMillis, windowMillis, intervalMillis, flags,
operation, recipientWrapper, listenerTag, workSource, alarmClock);
} catch (RemoteException ex) {
throw ex.rethrowFromSystemServer();
}
}
/**
* Available inexact recurrence interval recognized by
* {@link #setInexactRepeating(int, long, long, PendingIntent)}
* when running on Android prior to API 19.
*/
public static final long INTERVAL_FIFTEEN_MINUTES = 15 * 60 * 1000;
/**
* Available inexact recurrence interval recognized by
* {@link #setInexactRepeating(int, long, long, PendingIntent)}
* when running on Android prior to API 19.
*/
public static final long INTERVAL_HALF_HOUR = 2*INTERVAL_FIFTEEN_MINUTES;
/**
* Available inexact recurrence interval recognized by
* {@link #setInexactRepeating(int, long, long, PendingIntent)}
* when running on Android prior to API 19.
*/
public static final long INTERVAL_HOUR = 2*INTERVAL_HALF_HOUR;
/**
* Available inexact recurrence interval recognized by
* {@link #setInexactRepeating(int, long, long, PendingIntent)}
* when running on Android prior to API 19.
*/
public static final long INTERVAL_HALF_DAY = 12*INTERVAL_HOUR;
/**
* Available inexact recurrence interval recognized by
* {@link #setInexactRepeating(int, long, long, PendingIntent)}
* when running on Android prior to API 19.
*/
public static final long INTERVAL_DAY = 2*INTERVAL_HALF_DAY;
/**
* Schedule a repeating alarm that has inexact trigger time requirements;
* for example, an alarm that repeats every hour, but not necessarily at
* the top of every hour. These alarms are more power-efficient than
* the strict recurrences traditionally supplied by {@link #setRepeating}, since the
* system can adjust alarms' delivery times to cause them to fire simultaneously,
* avoiding waking the device from sleep more than necessary.
*
* <p>Your alarm's first trigger will not be before the requested time,
* but it might not occur for almost a full interval after that time. In
* addition, while the overall period of the repeating alarm will be as
* requested, the time between any two successive firings of the alarm
* may vary. If your application demands very low jitter, use
* one-shot alarms with an appropriate window instead; see {@link
* #setWindow(int, long, long, PendingIntent)} and
* {@link #setExact(int, long, PendingIntent)}.
*
* <p class="note">
* As of API 19, all repeating alarms are inexact. Because this method has
* been available since API 3, your application can safely call it and be
* assured that it will get similar behavior on both current and older versions
* of Android.
*
* @param type type of alarm.
* @param triggerAtMillis time in milliseconds that the alarm should first
* go off, using the appropriate clock (depending on the alarm type). This
* is inexact: the alarm will not fire before this time, but there may be a
* delay of almost an entire alarm interval before the first invocation of
* the alarm.
* @param intervalMillis interval in milliseconds between subsequent repeats
* of the alarm. Prior to API 19, if this is one of INTERVAL_FIFTEEN_MINUTES,
* INTERVAL_HALF_HOUR, INTERVAL_HOUR, INTERVAL_HALF_DAY, or INTERVAL_DAY
* then the alarm will be phase-aligned with other alarms to reduce the
* number of wakeups. Otherwise, the alarm will be set as though the
* application had called {@link #setRepeating}. As of API 19, all repeating
* alarms will be inexact and subject to batching with other alarms regardless
* of their stated repeat interval.
* @param operation Action to perform when the alarm goes off;
* typically comes from {@link PendingIntent#getBroadcast
* IntentSender.getBroadcast()}.
*
* @see android.os.Handler
* @see #set
* @see #cancel
* @see android.content.Context#sendBroadcast
* @see android.content.Context#registerReceiver
* @see android.content.Intent#filterEquals
* @see #ELAPSED_REALTIME
* @see #ELAPSED_REALTIME_WAKEUP
* @see #RTC
* @see #RTC_WAKEUP
* @see #INTERVAL_FIFTEEN_MINUTES
* @see #INTERVAL_HALF_HOUR
* @see #INTERVAL_HOUR
* @see #INTERVAL_HALF_DAY
* @see #INTERVAL_DAY
*/
public void setInexactRepeating(@AlarmType int type, long triggerAtMillis,
long intervalMillis, PendingIntent operation) {
setImpl(type, triggerAtMillis, WINDOW_HEURISTIC, intervalMillis, 0, operation, null,
null, null, null, null);
}
/**
* Like {@link #set(int, long, PendingIntent)}, but this alarm will be allowed to execute
* even when the system is in low-power idle (a.k.a. doze) modes. This type of alarm must
* <b>only</b> be used for situations where it is actually required that the alarm go off while
* in idle -- a reasonable example would be for a calendar notification that should make a
* sound so the user is aware of it. When the alarm is dispatched, the app will also be
* added to the system's temporary whitelist for approximately 10 seconds to allow that
* application to acquire further wake locks in which to complete its work.</p>
*
* <p>These alarms can significantly impact the power use
* of the device when idle (and thus cause significant battery blame to the app scheduling
* them), so they should be used with care. To reduce abuse, there are restrictions on how
* frequently these alarms will go off for a particular application.
* Under normal system operation, it will not dispatch these
* alarms more than about every minute (at which point every such pending alarm is
* dispatched); when in low-power idle modes this duration may be significantly longer,
* such as 15 minutes.</p>
*
* <p>Unlike other alarms, the system is free to reschedule this type of alarm to happen
* out of order with any other alarms, even those from the same app. This will clearly happen
* when the device is idle (since this alarm can go off while idle, when any other alarms
* from the app will be held until later), but may also happen even when not idle.</p>
*
* <p>Regardless of the app's target SDK version, this call always allows batching of the
* alarm.</p>
*
* @param type type of alarm.
* @param triggerAtMillis time in milliseconds that the alarm should go
* off, using the appropriate clock (depending on the alarm type).
* @param operation Action to perform when the alarm goes off;
* typically comes from {@link PendingIntent#getBroadcast
* IntentSender.getBroadcast()}.
*
* @see #set(int, long, PendingIntent)
* @see #setExactAndAllowWhileIdle
* @see #cancel
* @see android.content.Context#sendBroadcast
* @see android.content.Context#registerReceiver
* @see android.content.Intent#filterEquals
* @see #ELAPSED_REALTIME
* @see #ELAPSED_REALTIME_WAKEUP
* @see #RTC
* @see #RTC_WAKEUP
*/
public void setAndAllowWhileIdle(@AlarmType int type, long triggerAtMillis,
PendingIntent operation) {
setImpl(type, triggerAtMillis, WINDOW_HEURISTIC, 0, FLAG_ALLOW_WHILE_IDLE,
operation, null, null, null, null, null);
}
/**
* Like {@link #setExact(int, long, PendingIntent)}, but this alarm will be allowed to execute
* even when the system is in low-power idle modes. If you don't need exact scheduling of
* the alarm but still need to execute while idle, consider using
* {@link #setAndAllowWhileIdle}. This type of alarm must <b>only</b>
* be used for situations where it is actually required that the alarm go off while in
* idle -- a reasonable example would be for a calendar notification that should make a
* sound so the user is aware of it. When the alarm is dispatched, the app will also be
* added to the system's temporary whitelist for approximately 10 seconds to allow that
* application to acquire further wake locks in which to complete its work.</p>
*
* <p>These alarms can significantly impact the power use
* of the device when idle (and thus cause significant battery blame to the app scheduling
* them), so they should be used with care. To reduce abuse, there are restrictions on how
* frequently these alarms will go off for a particular application.
* Under normal system operation, it will not dispatch these
* alarms more than about every minute (at which point every such pending alarm is
* dispatched); when in low-power idle modes this duration may be significantly longer,
* such as 15 minutes.</p>
*
* <p>Unlike other alarms, the system is free to reschedule this type of alarm to happen
* out of order with any other alarms, even those from the same app. This will clearly happen
* when the device is idle (since this alarm can go off while idle, when any other alarms
* from the app will be held until later), but may also happen even when not idle.
* Note that the OS will allow itself more flexibility for scheduling these alarms than
* regular exact alarms, since the application has opted into this behavior. When the
* device is idle it may take even more liberties with scheduling in order to optimize
* for battery life.</p>
*
* @param type type of alarm.
* @param triggerAtMillis time in milliseconds that the alarm should go
* off, using the appropriate clock (depending on the alarm type).
* @param operation Action to perform when the alarm goes off;
* typically comes from {@link PendingIntent#getBroadcast
* IntentSender.getBroadcast()}.
*
* @see #set
* @see #setRepeating
* @see #setWindow
* @see #cancel
* @see android.content.Context#sendBroadcast
* @see android.content.Context#registerReceiver
* @see android.content.Intent#filterEquals
* @see #ELAPSED_REALTIME
* @see #ELAPSED_REALTIME_WAKEUP
* @see #RTC
* @see #RTC_WAKEUP
*/
public void setExactAndAllowWhileIdle(@AlarmType int type, long triggerAtMillis,
PendingIntent operation) {
setImpl(type, triggerAtMillis, WINDOW_EXACT, 0, FLAG_ALLOW_WHILE_IDLE, operation,
null, null, null, null, null);
}
/**
* Remove any alarms with a matching {@link Intent}.
* Any alarm, of any type, whose Intent matches this one (as defined by
* {@link Intent#filterEquals}), will be canceled.
*
* @param operation IntentSender which matches a previously added
* IntentSender. This parameter must not be {@code null}.
*
* @see #set
*/
public void cancel(PendingIntent operation) {
if (operation == null) {
final String msg = "cancel() called with a null PendingIntent";
if (mTargetSdkVersion >= Build.VERSION_CODES.N) {
throw new NullPointerException(msg);
} else {
Log.e(TAG, msg);
return;
}
}
try {
mService.remove(operation, null);
} catch (RemoteException ex) {
throw ex.rethrowFromSystemServer();
}
}
/**
* Remove any alarm scheduled to be delivered to the given {@link OnAlarmListener}.
*
* @param listener OnAlarmListener instance that is the target of a currently-set alarm.
*/
public void cancel(OnAlarmListener listener) {
if (listener == null) {
throw new NullPointerException("cancel() called with a null OnAlarmListener");
}
ListenerWrapper wrapper = null;
synchronized (AlarmManager.class) {
if (sWrappers != null) {
wrapper = sWrappers.get(listener);
}
}
if (wrapper == null) {
Log.w(TAG, "Unrecognized alarm listener " + listener);
return;
}
wrapper.cancel();
}
/**
* Set the system wall clock time.
* Requires the permission android.permission.SET_TIME.
*
* @param millis time in milliseconds since the Epoch
*/
@RequiresPermission(android.Manifest.permission.SET_TIME)
public void setTime(long millis) {
try {
mService.setTime(millis);
} catch (RemoteException ex) {
throw ex.rethrowFromSystemServer();
}
}
/**
* Sets the system's persistent default time zone. This is the time zone for all apps, even
* after a reboot. Use {@link java.util.TimeZone#setDefault} if you just want to change the
* time zone within your app, and even then prefer to pass an explicit
* {@link java.util.TimeZone} to APIs that require it rather than changing the time zone for
* all threads.
*
* <p> On android M and above, it is an error to pass in a non-Olson timezone to this
* function. Note that this is a bad idea on all Android releases because POSIX and
* the {@code TimeZone} class have opposite interpretations of {@code '+'} and {@code '-'}
* in the same non-Olson ID.
*
* @param timeZone one of the Olson ids from the list returned by
* {@link java.util.TimeZone#getAvailableIDs}
*/
@RequiresPermission(android.Manifest.permission.SET_TIME_ZONE)
public void setTimeZone(String timeZone) {
if (TextUtils.isEmpty(timeZone)) {
return;
}
// Reject this timezone if it isn't an Olson zone we recognize.
if (mTargetSdkVersion >= Build.VERSION_CODES.M) {
boolean hasTimeZone = false;
try {
hasTimeZone = ZoneInfoDB.getInstance().hasTimeZone(timeZone);
} catch (IOException ignored) {
}
if (!hasTimeZone) {
throw new IllegalArgumentException("Timezone: " + timeZone + " is not an Olson ID");
}
}
try {
mService.setTimeZone(timeZone);
} catch (RemoteException ex) {
throw ex.rethrowFromSystemServer();
}
}
/** @hide */
public long getNextWakeFromIdleTime() {
try {
return mService.getNextWakeFromIdleTime();
} catch (RemoteException ex) {
throw ex.rethrowFromSystemServer();
}
}
/**
* Gets information about the next alarm clock currently scheduled.
*
* The alarm clocks considered are those scheduled by any application
* using the {@link #setAlarmClock} method.
*
* @return An {@link AlarmClockInfo} object describing the next upcoming alarm
* clock event that will occur. If there are no alarm clock events currently
* scheduled, this method will return {@code null}.
*
* @see #setAlarmClock
* @see AlarmClockInfo
* @see #ACTION_NEXT_ALARM_CLOCK_CHANGED
*/
public AlarmClockInfo getNextAlarmClock() {
return getNextAlarmClock(mContext.getUserId());
}
/**
* Gets information about the next alarm clock currently scheduled.
*
* The alarm clocks considered are those scheduled by any application
* using the {@link #setAlarmClock} method within the given user.
*
* @return An {@link AlarmClockInfo} object describing the next upcoming alarm
* clock event that will occur within the given user. If there are no alarm clock
* events currently scheduled in that user, this method will return {@code null}.
*
* @see #setAlarmClock
* @see AlarmClockInfo
* @see #ACTION_NEXT_ALARM_CLOCK_CHANGED
*
* @hide
*/
public AlarmClockInfo getNextAlarmClock(int userId) {
try {
return mService.getNextAlarmClock(userId);
} catch (RemoteException ex) {
throw ex.rethrowFromSystemServer();
}
}
/**
* An immutable description of a scheduled "alarm clock" event.
*
* @see AlarmManager#setAlarmClock
* @see AlarmManager#getNextAlarmClock
*/
public static final class AlarmClockInfo implements Parcelable {
private final long mTriggerTime;
private final PendingIntent mShowIntent;
/**
* Creates a new alarm clock description.
*
* @param triggerTime time at which the underlying alarm is triggered in wall time
* milliseconds since the epoch
* @param showIntent an intent that can be used to show or edit details of
* the alarm clock.
*/
public AlarmClockInfo(long triggerTime, PendingIntent showIntent) {
mTriggerTime = triggerTime;
mShowIntent = showIntent;
}
/**
* Use the {@link #CREATOR}
* @hide
*/
AlarmClockInfo(Parcel in) {
mTriggerTime = in.readLong();
mShowIntent = in.readParcelable(PendingIntent.class.getClassLoader());
}
/**
* Returns the time at which the alarm is going to trigger.
*
* This value is UTC wall clock time in milliseconds, as returned by
* {@link System#currentTimeMillis()} for example.
*/
public long getTriggerTime() {
return mTriggerTime;
}
/**
* Returns an intent that can be used to show or edit details of the alarm clock in
* the application that scheduled it.
*
* <p class="note">Beware that any application can retrieve and send this intent,
* potentially with additional fields filled in. See
* {@link PendingIntent#send(android.content.Context, int, android.content.Intent)
* PendingIntent.send()} and {@link android.content.Intent#fillIn Intent.fillIn()}
* for details.
*/
public PendingIntent getShowIntent() {
return mShowIntent;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeLong(mTriggerTime);
dest.writeParcelable(mShowIntent, flags);
}
public static final @android.annotation.NonNull Creator<AlarmClockInfo> CREATOR = new Creator<AlarmClockInfo>() {
@Override
public AlarmClockInfo createFromParcel(Parcel in) {
return new AlarmClockInfo(in);
}
@Override
public AlarmClockInfo[] newArray(int size) {
return new AlarmClockInfo[size];
}
};
/** @hide */
public void writeToProto(ProtoOutputStream proto, long fieldId) {
final long token = proto.start(fieldId);
proto.write(AlarmClockInfoProto.TRIGGER_TIME_MS, mTriggerTime);
if (mShowIntent != null) {
mShowIntent.writeToProto(proto, AlarmClockInfoProto.SHOW_INTENT);
}
proto.end(token);
}
}
}
|