AlarmManager.java revision 54b6cfa9a9e5b861a9930af873580d6dc20f773c
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.content.Context;
20import android.content.Intent;
21import android.os.RemoteException;
22import android.os.ServiceManager;
23
24/**
25 * This class provides access to the system alarm services.  These allow you
26 * to schedule your application to be run at some point in the future.  When
27 * an alarm goes off, the {@link Intent} that had been registered for it
28 * is broadcast by the system, automatically starting the target application
29 * if it is not already running.  Registered alarms are retained while the
30 * device is asleep (and can optionally wake the device up if they go off
31 * during that time), but will be cleared if it is turned off and rebooted.
32 *
33 * <p><b>Note: The Alarm Manager is intended for cases where you want to have
34 * your application code run at a specific time, even if your application is
35 * not currently running.  For normal timing operations (ticks, timeouts,
36 * etc) it is easier and much more efficient to use
37 * {@link android.os.Handler}.</b>
38 *
39 * <p>You do not
40 * instantiate this class directly; instead, retrieve it through
41 * {@link android.content.Context#getSystemService
42 * Context.getSystemService(Context.ALARM_SERVICE)}.
43 */
44public class AlarmManager
45{
46    /**
47     * Alarm time in {@link System#currentTimeMillis System.currentTimeMillis()}
48     * (wall clock time in UTC), which will wake up the device when
49     * it goes off.
50     */
51    public static final int RTC_WAKEUP = 0;
52    /**
53     * Alarm time in {@link System#currentTimeMillis System.currentTimeMillis()}
54     * (wall clock time in UTC).  This alarm does not wake the
55     * device up; if it goes off while the device is asleep, it will not be
56     * delivered until the next time the device wakes up.
57     */
58    public static final int RTC = 1;
59    /**
60     * Alarm time in {@link android.os.SystemClock#elapsedRealtime
61     * SystemClock.elapsedRealtime()} (time since boot, including sleep),
62     * which will wake up the device when it goes off.
63     */
64    public static final int ELAPSED_REALTIME_WAKEUP = 2;
65    /**
66     * Alarm time in {@link android.os.SystemClock#elapsedRealtime
67     * SystemClock.elapsedRealtime()} (time since boot, including sleep).
68     * This alarm does not wake the device up; if it goes off while the device
69     * is asleep, it will not be delivered until the next time the device
70     * wakes up.
71     */
72    public static final int ELAPSED_REALTIME = 3;
73
74    private static IAlarmManager mService;
75
76    static {
77        mService = IAlarmManager.Stub.asInterface(
78                    ServiceManager.getService(Context.ALARM_SERVICE));
79    }
80
81    /**
82     * package private on purpose
83     */
84    AlarmManager() {
85    }
86
87    /**
88     * Schedule an alarm.  <b>Note: for timing operations (ticks, timeouts,
89     * etc) it is easier and much more efficient to use
90     * {@link android.os.Handler}.</b>  If there is already an alarm scheduled
91     * for the same IntentSender, it will first be canceled.
92     *
93     * <p>If the time occurs in the past, the alarm will be triggered
94     * immediately.  If there is already an alarm for this Intent
95     * scheduled (with the equality of two intents being defined by
96     * {@link Intent#filterEquals}), then it will be removed and replaced by
97     * this one.
98     *
99     * <p>
100     * The alarm is an intent broadcast that goes to an intent receiver that
101     * you registered with {@link android.content.Context#registerReceiver}
102     * or through the &lt;receiver&gt; tag in an AndroidManifest.xml file.
103     *
104     * <p>
105     * Alarm intents are delivered with a data extra of type int called
106     * {@link Intent#EXTRA_ALARM_COUNT Intent.EXTRA_ALARM_COUNT} that indicates
107     * how many past alarm events have been accumulated into this intent
108     * broadcast.  Recurring alarms that have gone undelivered because the
109     * phone was asleep may have a count greater than one when delivered.
110     *
111     * @param type One of ELAPSED_REALTIME, ELAPSED_REALTIME_WAKEUP, RTC or
112     *             RTC_WAKEUP.
113     * @param triggerAtTime Time the alarm should go off, using the
114     *                      appropriate clock (depending on the alarm type).
115     * @param operation Action to perform when the alarm goes off;
116     * typically comes from {@link PendingIntent#getBroadcast
117     * IntentSender.getBroadcast()}.
118     *
119     * @see android.os.Handler
120     * @see #setRepeating
121     * @see #cancel
122     * @see android.content.Context#sendBroadcast
123     * @see android.content.Context#registerReceiver
124     * @see android.content.Intent#filterEquals
125     * @see #ELAPSED_REALTIME
126     * @see #ELAPSED_REALTIME_WAKEUP
127     * @see #RTC
128     * @see #RTC_WAKEUP
129     */
130    public void set(int type, long triggerAtTime, PendingIntent operation) {
131        try {
132            mService.set(type, triggerAtTime, operation);
133        } catch (RemoteException ex) {
134        }
135    }
136
137    /**
138     * Schedule a repeating alarm.  <b>Note: for timing operations (ticks,
139     * timeouts, etc) it is easier and much more efficient to use
140     * {@link android.os.Handler}.</b>  If there is already an alarm scheduled
141     * for the same IntentSender, it will first be canceled.
142     *
143     * <p>Like {@link #set}, except you can also
144     * supply a rate at which the alarm will repeat.  This alarm continues
145     * repeating until explicitly removed with {@link #cancel}.  If the time
146     * occurs in the past, the alarm will be triggered immediately, with an
147     * alarm count depending on how far in the past the trigger time is relative
148     * to the repeat interval.
149     *
150     * <p>If an alarm is delayed (by system sleep, for example, for non
151     * _WAKEUP alarm types), a skipped repeat will be delivered as soon as
152     * possible.  After that, future alarms will be delivered according to the
153     * original schedule; they do not drift over time.  For example, if you have
154     * set a recurring alarm for the top of every hour but the phone was asleep
155     * from 7:45 until 8:45, an alarm will be sent as soon as the phone awakens,
156     * then the next alarm will be sent at 9:00.
157     *
158     * <p>If your application wants to allow the delivery times to drift in
159     * order to guarantee that at least a certain time interval always elapses
160     * between alarms, then the approach to take is to use one-time alarms,
161     * scheduling the next one yourself when handling each alarm delivery.
162     *
163     * @param type One of ELAPSED_REALTIME, ELAPSED_REALTIME_WAKEUP}, RTC or
164     *             RTC_WAKEUP.
165     * @param triggerAtTime Time the alarm should first go off, using the
166     *                      appropriate clock (depending on the alarm type).
167     * @param interval Interval between subsequent repeats of the alarm.
168     * @param operation Action to perform when the alarm goes off;
169     * typically comes from {@link PendingIntent#getBroadcast
170     * IntentSender.getBroadcast()}.
171     *
172     * @see android.os.Handler
173     * @see #set
174     * @see #cancel
175     * @see android.content.Context#sendBroadcast
176     * @see android.content.Context#registerReceiver
177     * @see android.content.Intent#filterEquals
178     * @see #ELAPSED_REALTIME
179     * @see #ELAPSED_REALTIME_WAKEUP
180     * @see #RTC
181     * @see #RTC_WAKEUP
182     */
183    public void setRepeating(int type, long triggerAtTime, long interval,
184            PendingIntent operation) {
185        try {
186            mService.setRepeating(type, triggerAtTime, interval, operation);
187        } catch (RemoteException ex) {
188        }
189    }
190
191    /**
192     * Remove any alarms with a matching {@link Intent}.
193     * Any alarm, of any type, whose Intent matches this one (as defined by
194     * {@link Intent#filterEquals}), will be canceled.
195     *
196     * @param operation IntentSender which matches a previously added
197     * IntentSender.
198     *
199     * @see #set
200     */
201    public void cancel(PendingIntent operation) {
202        try {
203            mService.remove(operation);
204        } catch (RemoteException ex) {
205        }
206    }
207
208    public void setTimeZone(String timeZone) {
209        try {
210            mService.setTimeZone(timeZone);
211        } catch (RemoteException ex) {
212        }
213    }
214}
215