Android alarm notification cycle source code (AlarmManager)

Source: Internet
Author: User

The Android system provides the AlarmManager class to manage scheduled alarm notification tasks. You can use AlarmManager to implement scheduled reminders and periodic notifications. The AlarmManager class can be applied to the following scenarios:
1. Scheduled cyclic startup components (Component, such as Activity and BroadcastReceiver) can be used to start the Service in the background for scheduled reminder tasks.
2. Implement scheduled cycle reminders by hour, day, or week.
The timed start component is very simple. The following shows the core code of the timed cycle reminder function in the form of daily and weekly alarms. The core of this function is to calculate the next alarm reminder time. The Code is as follows:
/**
* Three alarm modes (dateMode ):
* 1. DATE_MODE_FIX: Specifies a date, for example, 20120301. Format of the parameter dateValue:
* 2. DATE_MODE_WEEK: a weekly reminder, for example, Monday or Wednesday. The format of the parameter dateValue is 1, 3.
* 3. DATE_MODE_MONTH: monthly reminder, for example, March 13, March 3, and March 3. Format of the parameter dateValue: |
*
* StartTime: the start time of the current day, for example, nine o'clock A.M.. The parameter format is.
*/
Public static long getNextAlarmTime (int dateMode, String dateValue,
String startTime ){
Final SimpleDateFormat fmt = new SimpleDateFormat ();
Final Calendar c = Calendar. getInstance ();
Final long now = System. currentTimeMillis ();

// Set the start time
Try {
If (Task. DATE_MODE_FIX = dateMode ){
Fmt. applyPattern ("yyyy-MM-dd ");
Date d = fmt. parse (dateValue );
C. setTimeInMillis (d. getTime ());
}

Fmt. applyPattern ("HH: mm ");
Date d = fmt. parse (startTime );
C. set (Calendar. HOUR_OF_DAY, d. getHours ());
C. set (Calendar. MINUTE, d. getMinutes ());
C. set (Calendar. SECOND, 0 );
C. set (Calendar. MILLISECOND, 0 );
} Catch (Exception e ){
E. printStackTrace ();
}

Long nextTime = 0;
If (Task. DATE_MODE_FIX = dateMode) {// Based on the specified date
NextTime = c. getTimeInMillis ();
// The specified date has passed
If (now> = nextTime) nextTime = 0;
} Else if (Task. DATE_MODE_WEEK = dateMode) {// By week
Final long [] checkedWeeks = parseDateWeeks (dateValue );
If (null! = CheckedWeeks ){
For (long week: checkedWeeks ){
C. set (Calendar. DAY_OF_WEEK, (int) (week + 1 ));

Long triggerAtTime = c. getTimeInMillis ();
If (triggerAtTime <= now) {// next week
TriggerAtTime + = AlarmManager. INTERVAL_DAY * 7;
}
// Save the latest alarm time
If (0 = nextTime ){
NextTime = triggerAtTime;
} Else {
NextTime = Math. min (triggerAtTime, nextTime );
}
}
}
} Else if (Task. DATE_MODE_MONTH = dateMode) {// monthly
Final long [] [] items = parseDateMonthsAndDays (dateValue );
Final long [] checkedMonths = items [0];
Final long [] checkedDays = items [1];

If (null! = CheckedDays & null! = CheckedMonths ){
Boolean isAdd = false;
For (long month: checkedMonths ){
C. set (Calendar. MONTH, (int) (month-1 ));
For (long day: checkedDays ){
C. set (Calendar. DAY_OF_MONTH, (int) day );

Long triggerAtTime = c. getTimeInMillis ();
If (triggerAtTime <= now) {// next year
C. add (Calendar. YEAR, 1 );
TriggerAtTime = c. getTimeInMillis ();
IsAdd = true;
} Else {
IsAdd = false;
}
If (isAdd ){
C. add (Calendar. YEAR,-1 );
}
// Save the latest alarm time www.2cto.com
If (0 = nextTime ){
NextTime = triggerAtTime;
} Else {
NextTime = Math. min (triggerAtTime, nextTime );
}
}
}
}
}
Return nextTime;
}

Public static long [] parseDateWeeks (String value ){
Long [] weeks = null;
Try {
Final String [] items = value. split (",");
Weeks = new long [items. length];
Int I = 0;
For (String s: items ){
Weeks [I ++] = Long. valueOf (s );
}
} Catch (Exception e ){
E. printStackTrace ();
}
Return weeks;
}

Public static long [] [] parseDateMonthsAndDays (String value ){
Long [] [] values = new long [2] [];
Try {
Final String [] items = value. split ("\\| ");
Final String [] monthStrs = items [0]. split (",");
Final String [] dayStrs = items [1]. split (",");
Values [0] = new long [monthStrs. length];
Values [1] = new long [dayStrs. length];

Int I = 0;
For (String s: monthStrs ){
Values [0] [I ++] = Long. valueOf (s );
}
I = 0;
For (String s: dayStrs ){
Values [1] [I ++] = Long. valueOf (s );
}
} Catch (Exception e ){
E. printStackTrace ();
}
Return values;
}
If you do not close the alarm after the alarm is triggered, you can use the setRepeating () method of AlarmManager to remind you again every few minutes. Otherwise, use set. The alarm program will also use the screen unlocking function. For details, see the Android screen lighting (always on) and screen unlocking and locking.

 

From the blog of radish Cabbage

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.