Android Setup Alarm steps and Base code

Source: Internet
Author: User

Three main steps: 1. Set the alarm time; 2. Receive alarm event broadcast; 3. Re-calculate and set the alarm time after reboot; 1. Set the alarm time (in milliseconds)
Private voidSetalarmtime (Context context,LongTimeinmillis) {Alarmmanager am=(Alarmmanager) Context.getsystemservice (Context.alarm_service); Intent Intent=NewIntent ("android.alarm.demo.action"); Pendingintent Sender=pendingintent.getbroadcast (Context,0, intent, pendingintent.flag_cancel_current); intInterval = -* +;//Alarm interval, set to 1 minutes to make a noise, in the 2nd step we will receive a broadcast every 1 minutesam.setrepeating (Alarmmanager.rtc_wakeup, Timeinmillis, Interval, sender)}

2. Receive Alarm event broadcasts

 Public class Alarmreceiver extends Broadcastreceiver {  publicvoid  onreceive (context context, Intent Intent) {  if  ("Android.alarm.demo.action". Equals (Intent.getaction ())) {     // The alarm time is set in the 1th step, where you can eject the alarm prompt and play    the bell // you can continue to set the next alarm time;    return ;  }  }}

Of course, receiver is required to be registered in the Manifest.xml:

<receiver android:name="alarmreceiver">    <intent-filter>      < Action android:name="android.alarm.demo.action" />    </intent-filter></ Receiver>

3. Re-calculate and set the alarm time after reboot of course there is a bootreceiver:

 Public class Bootreceiver extends Broadcastreceiver {  publicvoid  onreceive (context context, Intent Intent) {  = intent.getaction ();   if (Action.equals (intent.action_boot_completed)) {    // recalculate alarm time, and adjust the first step to set the alarm time and alarm interval   }   }}

Of course, you also need to register:

<receiver android:name="bootreceiver">    <intent-filter>      < Action android:name="android.intent.action.BOOT_COMPLETED" />    </ Intent-filter></receiver>

The principle of the alarm clock is actually so much, as to specific details such as alarm time storage and calculation, interface display and alarm prompt way, everyone's ideas will be different

Android Setup Alarm steps and Base code

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.