Alert clock Learning

Source: Internet
Author: User

I recently looked at the alarm management class (alarmmanager) of Android, which is powerful and simple,CodeAs follows:

1. Create an alarmreceiver to inherit from broadcastreceiver and declare it in androidmanifest. xml.
Public static class alarmreceiver extends broadcastreceiver {
@ Override
Public void onreceive (context, intent ){
Toast. maketext (context, "alarm prompt: time is up! ", Toast. length_long). Show ();
}
}

2. Create intent and pendingintent to call the target component.
Intent intent = new intent (this, alarmreceiver. Class );
Pendingintent = pendingintent. getbroadcast (this, 0, intent, 0 );

3. set an alarm
Get the alarm management instance:
Alarmmanager = (alarmmanager) getsystemservice (context. alarm_service );

Set a single alarm:
Alarmmanager. Set (alarmmanager. rtc_wakeup, system. currenttimemillis () + (5*1000), pendingintent );

Set the cycle alarm:
Alarmmanager. setrepeating (alarmmanager. rtc_wakeup, system. currenttimemillis () + (10*1000), (24*60*60*1000), pendingintent );

 

3-4:

Re-calculate and set the alert time after reboot

Of course there must be a bootreceiver:
Public class bootreceiver extends broadcastreceiver {
Public void onreceive (context, intent ){
String action = intent. getaction ();
If (action. Equals (intent. action_boot_completed )){
// Recalculate the alert time, and set the alert time and alert interval in step 1.
Alarmmanager AM = (alarmmanager) Context. getsystemservice (context. alarm_service );


Intent intent = new intent ("intent. action_request_shutdown ");

Pendingintent sender = pendingintent. getbroadcast (context, 0, intent, pendingintent. flag_cancel_current );

Int interval = 60*1000; // alert interval. this parameter is set to 1 minute. in step 1, we will receive a broadcast every 1 minute.

Am. Set (alarmmanager. rtc_wakeup, timeinmillis, interval, Sender)

}< BR >}< br> Of course, you also need to register:




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.