The Alarm controller demonstrates how to use the Alarm event in an Android app, with features similar to Java.util.Timer, TimerTask. But alarm can do this even after the current application exits schedule a task is executed at the specified time.
Alarmmanager is used to manage alarm events and supports single or recurring executions. As with the majority of Android services, Alarmmanager also obtains the service object through Getsystemservice:
Alarmmanager am = (alarmmanager) getsystemservice (Alarm_service);
The task description type for timertask corresponds to Pendingintent,pendingintent, which describes the intent,pendingintent that will be executed without providing a constructor, which needs to be getactivity through the static function ( context, int, Intent, int), getbroadcast (context, int, Intent, int), GetService (context, int, Intent, int) come or want to execute Activit Y,broadcast,service description.
In this example, the descriptions of broadcast oneshotalarm and repeatingalarm are obtained, corresponding to the broadcast events executed at a single execution and the broadcast events on multiple executions, which are Androidmanifest.xml defined as broadcast Receiver:
<receiver android:name= ". App. Oneshotalarm "android:process=": Remote "/><receiver android:name=". App. Repeatingalarm "android:process=": Remote "/>
Schedule one-time alarm event code is as follows:
//When the alarm goes off, we want to broadcast a Intent to our//Broadcastreceiver. Here we make a Intent with an explicit class//name to own receiver (which had been published in//Androidmanifest.xml) instantiated and called, and then create an//Intentsender to the intent executed as a broadcast.Intent Intent =NewIntent (Alarmcontroller. This, Oneshotalarm.class); Pendingintent Sender=Pendingintent.getbroadcast(Alarmcontroller. This, 0, Intent, 0); //We want the alarm to go off for seconds from now.Calendar Calendar =calendar.getinstance (); Calendar.settimeinmillis (System.currenttimemillis ()); Calendar.add (Calendar.second,30); //Schedule the alarm!Alarmmanager am =(Alarmmanager) Getsystemservice (Alarm_service); Am.set (Alarmmanager.rtc_wakeup, Calendar.gettimeinmillis (), sender);
Where sender is a description of the intent sent to broadcast Receiver Oneshotalarm, when the specified time is reached (30 seconds in the example), Alarmmanager will send Oneshotalarm a broadcast When Intent,oneshotalarm is received, a message is displayed on the screen using a toast. If you click "One Shot Alarm" more than once, it will not schedule multiple Alarm events, because schedule the same sender object, the next time this scheduled event will be canceled.
Schedule a repeating event code is as follows:
//When the alarm goes off, we want to broadcast a Intent to our//Broadcastreceiver. Here we make a Intent with an explicit class//name to own receiver (which had been published in//Androidmanifest.xml) instantiated and called, and then create an//Intentsender to the intent executed as a broadcast. //Note That unlike above, this intentsender is configured to//Allow itself to be sent multiple times.Intent Intent =NewIntent (Alarmcontroller. This, Repeatingalarm.class); Pendingintent Sender= Pendingintent.getbroadcast (Alarmcontroller. This, 0, Intent, 0); //We want the alarm to go off for seconds from now. LongFirsttime =Systemclock.elapsedrealtime (); Firsttime+ = 15*1000; //Schedule the alarm!Alarmmanager am =(Alarmmanager) Getsystemservice (Alarm_service); Am.setrepeating (Alarmmanager.elapsed_realtime_wakeup, Firsttime,15*1000, sender);
The above code sends a broadcast event to repeatingalarm every 15 seconds, and a message is displayed on the screen after Repeatingalarm accepts it.
For events with schedule, one or more times, you can call Alarmmanager's Cancel method to cancel the schedule alarm event, and the following code cancels multiple alarm events.
//Create the same intent, and thus a matching intentsender, for//The one is scheduled.Intent Intent =NewIntent (Alarmcontroller. This, Repeatingalarm.class); Pendingintent Sender= Pendingintent.getbroadcast (Alarmcontroller. This, 0, Intent, 0); //And cancel the alarm.Alarmmanager am =(Alarmmanager) Getsystemservice (Alarm_service); Am.cancel (sender);
Note: If you do not cancel this multiple alarm event, a message will be displayed on the screen every 15 seconds, even if you exit this example or launch another application until the reboot is aborted.
"Sail Plan 017" 2015 sail plan Android Apidemo Devil Step app->alarm->alarm Controller Alarm event pendingintent Schedule Alarmmanager