Android Daily Timer alert feature implementation

Source: Internet
Author: User
Tags set time

Android to achieve timed function that must use the technology of the alarm,

Then the Android alarm implementation is based on the Alarmmanager class, first we look at its several main methods.

Open the Alarmmanager source code, as shown:

Two core methods:

Private final Ialarmmanager Mservice;
 Public void Set (intlong  triggeratmillis, pendingintent operation) {      try  {                                                                           mservice . Set (type, triggeratmillis, operation);                          Catch (RemoteException ex) {                                              }                                                                       }               

And

 public  void  setrepeating (int  type, long   Triggeratmillis,  long   Intervalmillis, Pendingint ent operation) { try   {mservice.setrepeating (type, TRIGGERATMI     Llis, Intervalmillis, operation);                                                    catch   (RemoteException ex) {                                                                               }                                                                            } 

The main function of the first method is to register a relatively simple alarm, the second method is to register a duplicate alarm, where repeated means: Set 5s, then every 5s will be executed once.

We see here the specific implementation is based on the Ialarmmanager, and Ialarmmanager is a aidl (Android Interface definition Language), the specific is not described in detail, We are interested to study it by ourselves.

Let me take a look at how the set (int type, long triggeratmillis, pendingintent operation) method is called:

//make an alarm registrationIntent Intent =NewIntent (mainactivity. This, Alarmreceiver.class); Pendingintent Sender= Pendingintent.getbroadcast (mainactivity. This,0, Intent,0);//over 10s to execute this alarmCalendar Calendar =calendar.getinstance (); Calendar.settimeinmillis (System.currenttimemillis ()); Calendar.add (Calendar.SECOND, Ten); Alarmmanager Manager=(Alarmmanager) Getsystemservice (alarm_service); manager.Set(Alarmmanager.rtc_wakeup, Calendar.gettimeinmillis (), sender);

Look at the source code of Alarmreceiver:

/* * * *   @ClassName: Alarmreceiver   * @Description: Alarm time will come into this broadcast, this time can do some business to do. * @author Huhood * @date 2013-11-25 PM 4:44:30   * * Publicclass  alarmreceiver Extends Broadcastreceiver {        @Override    publicvoid  onreceive (Context Context, Intent Intent) {    " the alarm rang, can do something ~ ~", Toast.length_long). Show ();    }}

And don't forget, androidmanifest.xml need to join in:

<receiver android:name="com.example.alarmmanagerdemo.AlarmReceiver" android: process=": Remote">

After running, after 10s pop-up "alarm rang, you can do something ~ ~", the description of success.

OK, this result is definitely not what we want, we want the function is a daily reminder function, then need to be based on

setrepeating (int type, long triggeratmillis,long intervalmillis, pendingintent operation)

This method is implemented with the following code:

Intent Intent =NewIntent (mainactivity. This, Alarmreceiver.class); Pendingintent Sender= Pendingintent.getbroadcast (mainactivity. This,0, Intent,0);LongFirsttime = Systemclock.elapsedrealtime ();//running time (including sleep time) after bootLongSystemTime =System.currenttimemillis (); Calendar Calendar=calendar.getinstance (); Calendar.settimeinmillis (System.currenttimemillis ());//The time zone needs to be set up here, or there will be a 8 hour difference.Calendar.settimezone (Timezone.gettimezone ("gmt+8") ); calendar.Set(Calendar.minute, Mminute); Calendar.Set(Calendar.hour_of_day, mhour); Calendar.Set(Calendar.second,0); calendar.Set(Calendar.millisecond,0);//timing time of the selectionLongSelecttime =Calendar.gettimeinmillis ();//If the current time is greater than the set time, start from the next day's set timeif(SystemTime >selecttime) {Toast.maketext (mainactivity). This,"set time is less than current time", Toast.length_short). Show (); Calendar.add (Calendar.day_of_month,1); Selecttime=Calendar.gettimeinmillis ();}//Calculate the time difference between now and the set timeLongTime = Selecttime-Systemtime;firsttime+=Time ;//make an alarm registrationAlarmmanager Manager =(Alarmmanager) Getsystemservice (Alarm_service); Manager.setrepeating (Alarmmanager.elapsed_realtime_wakeup, Firsttime, day, sender); LOG.I (TAG,"Time = ="+ Time +", Selecttime ====="+ Selecttime +", SystemTime = ="+ SystemTime +", Firsttime = = ="+firsttime); Toast.maketext (mainactivity. This,"set the repeat alarm to succeed!", Toast.length_long). Show ();

The above idea is roughly like this, first of all, according to set the time, calculate the current time away from the set time difference, plus the time difference, we know the first reminder, and then set the time interval of repetition, we set here 1 days on the can.

Now explain the first parameter in setrepeating:

ALARMMANAGER.RTC, the hardware alarm clock, does not wake up the phone (or other devices) hibernate, when the phone does not fire alarm when hibernating.
Alarmmanager.rtc_wakeup, hardware alarm clock, when the alarm clock hair 躰 wake up the phone sleep;
Alarmmanager.elapsed_realtime, the real time passes the alarm clock, does not wake the phone to sleep, when the phone sleeps, does not emit the alarm clock.
Alarmmanager.elapsed_realtime_wakeup, real time elapsed alarm clock, when the alarm clock hair 躰 wake up the phone sleep;
The biggest difference between RTC alarms and Elapsed_realtime is that the former can trigger alarm events by modifying the phone's time, which passes through the real time, even in the dormant state, and the time is calculated.

Also, we are here to call the form of broadcasting to achieve, can also be implemented using the service, as long as the pendingintent changes can be, the relevant code:
Pendingintent.getservice (Mainactivity.this, 0,new Intent (Mainactivity.this,alarmservice.class), 0);
The others are the same.

Click here: Code

Android Daily Timer alert feature implementation

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.