Custom alert clock in Android

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.