This example describes the basic usage of Alarmmanager in Android. Share to everyone for your reference, specific as follows:
The explanation in the Alarmmanager's role document is that we broadcast a specified intent at a particular time. The simple thing is that we set a time, and then when that time arrives, Alarmmanager broadcasts us a set of intent.
A deeper understanding of the Alarmmanager can refer to:
Http://www.jb51.net/article/90491.htm
Android offers four different types of alarms:
①elapsed_realtime
Sends a broadcast after the specified delay, but does not wake the device.
②elapsed_realtime_wakeup
After the specified demo, send the broadcast and wake the device
Delay is to put the system start Time systemclock.elapsedrealtime (), the specific use of the code.
③rtc
At the specified moment, the broadcast is sent, but the device is not awakened
④rtc_wakeup
At the specified moment, send the broadcast, and wake the device
Alarmmanager provides the following methods:
1. void set (int type, long triggerattime, pendingintent operation)
Set an alarm clock
2. void setrepeating (int type, long triggerattime, long interval, pendingintent operation)
Set an alarm clock to be repeated
3. void setinexactrepeating (int type, long triggerattime, long interval, pendingintent operation)
Set an imprecise version of a duplicate alarm clock, which is relatively more energy-efficient (power-efficient) because the system may merge several similar alarms into one to perform and reduce the number of wake-up times for the device.
The built-in several interval are:
Interval_fifteen_minutes
Interval_half_hour
Interval_hour
Interval_half_day
Interval_day
If you set it as day, all of the alarm clocks in that date will be merged.
void Cancel (Pendingintent operation)
Cancel a set of alarm clock
void Settimezone (String timeZone)
Sets the default time zone for the system. Requires Android.permission.SET_TIME_ZONE permissions
First create receiver public
class Alarmreceiver extends Broadcastreceiver {
@Override public
void OnReceive ( Context, Intent Intent) {
Toast.maketext (context, "alarm", Toast.length_short). Show ();
}
Manifest stated that there is no need for intent-filter, we are expressly assigned to which receiver
<receiver android:name= " Yuan.receivers.AlarmReceiver "/>
Pendingintent: Simply put a specified action on the intent. Intent, we also need to perform startactivity, StartService or sendbroadcast to make intent useful. And Pendingintent's words are included in the action, such as Pendingintent.getbroadcast contains the Sendbroadcast action.
Send the specified broadcast after 5s
Alarmmanager alarmmgr = (alarmmanager) getsystemservice (context.alarm_service);
Intent Intent = new Intent (Getapplicationcontext (), alarmreceiver.class);
int requestcode = 0;
Pendingintent pendintent = Pendingintent.getbroadcast (Getapplicationcontext (),
Requestcode, intent, pendingintent.flag_update_current);
Send the broadcast in 5 seconds, send only once
int triggerattime = systemclock.elapsedrealtime () + 5 * 1000;
Alarmmgr.set (Alarmmanager.elapsed_realtime, Triggerattime, pendintent);
Send the specified broadcast after 5s, then repeat the broadcast every 10 seconds
Alarmmanager alarmmgr = (alarmmanager) getsystemservice (context.alarm_service);
Intent Intent = new Intent (Getapplicationcontext (), alarmreceiver.class);
int requestcode = 0;
Pendingintent pendintent = Pendingintent.getbroadcast (Getapplicationcontext (),
Requestcode, intent, pendingintent.flag_update_current);
Send the broadcast in 5 seconds, then repeat every 10 seconds. Broadcasts are sent directly to the alarmreceiver
int triggerattime = systemclock.elapsedrealtime () + 5 * 1000;
int interval = ten * 1000;
Alarmmgr.setrepeating (Alarmmanager.elapsed_realtime_wakeup, Triggerattime, Interval, pendintent);
Cancel an alarm clock
Alarmmanager alarmmgr = (alarmmanager) getsystemservice (context.alarm_service);
Intent Intent = new Intent (Getapplicationcontext (), alarmreceiver.class);
Pendingintent pendintent = Pendingintent.getbroadcast (Getapplicationcontext (),
0, intent, pendingintent.flag_ update_current);
The alarm clock that matches the above intent (filterequals (intent)) will be canceled
Alarmmgr.cancel (pendintent);
For more information on Android-related content readers can view the site topics: "Android Development Introduction and Advanced Course", "Android debugging techniques and common problems solution summary", "Android Multimedia operating skills Summary (audio, video, recording, etc.)", " Android Basic Components Usage Summary, Android View tips Summary, Android layout layout tips and Android Control usage summary
I hope this article will help you with the Android program.