The use mechanism of Alarmmanager is called the global timer, and some call it an alarm clock. Through the use of it, the personal feel called the global timer is more appropriate, in fact, its role and timer a bit similar. There are two similar usages: (1) Perform an action after a specified length of time (2) perform an operation periodically
Alarmmanager objects are used in conjunction with intent, you can start an activity on a timed basis, send a broadcast, or open a service.
The following code describes the use of two timing methods in detail:
(1) Perform an action after a specified length of time
Code
Action: Send a broadcast, broadcast received after the toast prompt timed operation completed
Intent Intent =new Intent (Main.this, Alarmreceiver.class);
Intent.setaction ("short");
Pendingintent sender=
Pendingintent.getbroadcast (main.this, 0, intent, 0);
Set a time after five seconds.
Calendar calendar=calendar.getinstance ();
Calendar.settimeinmillis (System.currenttimemillis ());
Calendar.add (Calendar.second, 5);
Alarmmanager alarm= (Alarmmanager) Getsystemservice (Alarm_service);
Alarm.set (Alarmmanager.rtc_wakeup, Calendar.gettimeinmillis (), sender);
or simplify it in the following way
Alarm.set (Alarmmanager.rtc_wakeup, System.currenttimemillis () +5*1000, sender);
Toast.maketext (Main.this, "alarm open after five seconds", Toast.length_long). Show ();
Note: Receiver remembers registering in Manifest.xml
Codepublic static class Alarmreceiver extends broadcastreceiver{
@Override
public void OnReceive (context context, Intent Intent) {
TODO auto-generated Method Stub
if (Intent.getaction (). Equals ("short")) {
Toast.maketext (Context, "short alarm", Toast.length_long). Show ();
}else{
Toast.maketext (Context, "repeating alarm", toast.length_long). Show ();
}
}
}
(2) Periodic execution of an operation
CodeIntent Intent =new Intent (Main.this, Alarmreceiver.class);
Intent.setaction ("repeating");
Pendingintent sender=pendingintent
. Getbroadcast (main.this, 0, intent, 0);
Start time
Long Firstime=systemclock.elapsedrealtime ();
Alarmmanager am= (Alarmmanager) Getsystemservice (Alarm_service); 5 seconds, one cycle, no stop sending broadcasts.
Am.setrepeating (alarmmanager.elapsed_realtime_wakeup
, Firstime, 5*1000, sender);
Alarmmanager's setrepeating () is equivalent to a timer's schedule (task,delay,peroid); a bit of a difference where the timer this method is to specify how long to delay
Start the periodic execution task later;
Alarmmanager Cancellation: (It is important to note that the canceled intent must be in absolute agreement with the startup intent to support the cancellation Alarmmanager)
CodeIntent Intent =new Intent (Main.this, Alarmreceiver.class);
Intent.setaction ("repeating");
Pendingintent sender=pendingintent
. Getbroadcast (main.this, 0, intent, 0);
Alarmmanager alarm= (Alarmmanager) Getsystemservice (Alarm_service);
Alarm.cancel (sender);
Android---alarmmanager (global timer/alarm) Specify the length of time or perform an action in a periodic manner