The alarm clock is a common tool in our life. The Android system provides powerful alarm clock interfaces and is easy to use. Alarmmanager is an alarm management interface provided by Android. We want to create an alertProgramIt is needed. Next, let's take a look at the process of creating an alarm:
1. Get the alarmmanager object
Alarmmanager AM = (alarmmanager) getsystemservice (context. alarm_service );
Getsystemservice (servicecode) is used to obtain system services in Android ).
2. Create an alert er Class for alarmmanager. This class is used to receive broadcast alert events. We can also think of it as an alert handler.
Public ClassAlarmreceiverExtendsBroadcastreceiver {
@ Override
Public VoidOnreceive (context, intent ){
Toast. maketext (context, "this the time", Toast. length_long). Show ();
}
}
This class must inherit from the broadcastreceiver class. There are two ways to receive intent broadcast events: one is to dynamically register a receiver, and the other is to create a class inherited from broadcastreceiver, and in androidmanifest. add reference of this class to the XML file, the following green font
<Cycler Android: Name = ". alarmreceiver" Android: Process = ": Remote"/>
< Application Android: icon = "@ Drawable/icon" Android: Label = "@ String/app_name" >
< Activity Android: Name = ". Alarmshowactivity"
Android: Label = "@ String/app_name" >
< Intent-Filter >
< Action Android: Name = "Android. Intent. Action. Main" />
< Category Android: Name = "Android. Intent. Category. launcher" />
</ Intent-Filter >
</ Activity >
<Cycler Android: Name = "alarmreceiver">
</ Cycler >
</ Application >
3. Create two intents, one for alarmreceiver class processing and the other for broadcast.
Intent intent =NewIntent (alarmshowactivity.This, Alarmreceiver.Class);
Intent. setaction ("alarm_action ");
Pendingintent = pendingintent. getbroadcast (alarmshowactivity.This, 0, intent, 0 );
4. Finally, we need to set the parameters that really enable alarm to work.
Am. Set (alarmmanager.Rtc_wakeup, _ C. gettimeinmillis (), pendingintent );
Am. setrepeating (alarmmanager. rtc_wakeup, system. currenttimemillis () + 10*1000, 60*60*1000, pendingintent );
The parameters in the red font are important. This is how alarm occurs. Here, the UTC time is used as the benchmark and the device mode is wake up when alarm occurs. For more modes, refer to this link.