--- /dev/null
+package com.ic.work.utils;
+
+import android.app.AlarmManager;
+import android.app.PendingIntent;
+import android.content.Context;
+import android.content.Intent;
+import android.os.Build;
+
+import java.util.Calendar;
+import java.util.Date;
+
+public class AlarmManagerUtil {
+
+ public static final String ALARM_ACTION = "com.ic.alarm.action.BOARD_TEMPERATURE_CHECK";
+
+ public static void setAlarmTime(Context context, long timeInMillis, Intent intent) {
+ AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
+ PendingIntent sender = PendingIntent.getBroadcast(context, intent.getIntExtra("id", 0),
+ intent, PendingIntent.FLAG_CANCEL_CURRENT);
+ int interval = (int) intent.getLongExtra("intervalMillis", 0);
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
+ am.setWindow(AlarmManager.RTC_WAKEUP, timeInMillis, interval, sender);
+ }
+ }
+
+ public static void cancelAlarm(Context context, String action, int id) {
+ Intent intent = new Intent(action);
+ PendingIntent pi = PendingIntent.getBroadcast(context, id, intent, PendingIntent
+ .FLAG_CANCEL_CURRENT);
+ AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
+ am.cancel(pi);
+ }
+
+ /**
+ * @param flag 周期性时间间隔的标志,flag = 0 表示一次性的闹钟, flag = 1 表示每天提醒的闹钟(1天的时间间隔),flag = 2
+ * 表示按周每周提醒的闹钟(一周的周期性时间间隔)
+ * @param hour 时
+ * @param minute 分
+ * @param id 闹钟的id
+ * @param week week=0表示一次性闹钟或者按天的周期性闹钟,非0 的情况下是几就代表以周为周期性的周几的闹钟
+ * @param tips 闹钟提示信息
+ * @param soundOrVibrator 2表示声音和震动都执行,1表示只有铃声提醒,0表示只有震动提醒
+ */
+ public static void setAlarm(Context context, int flag, int hour, int minute, int id, int
+ week, String tips, int soundOrVibrator, String action, long intervalMillis) {
+ ICLogger.d("flag ==> " + flag + " hour ==> " + hour + " minute ==> " +
+ minute + " week ==> " + week + " tips==> " +
+ tips + " soundOrVibrator==> " + soundOrVibrator + " intervalMillis==> " + intervalMillis);
+ AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
+ Calendar calendar = Calendar.getInstance();
+ calendar.setTime(new Date());
+ calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get
+ (Calendar.DAY_OF_MONTH), hour, minute, 0);
+ if (flag == 1) {
+ intervalMillis = 24 * 60 * 60 * 1000;
+ } else if (flag == 2) {
+ intervalMillis = 24 * 60 * 60 * 1000 * 7;
+ }
+ Intent intent = new Intent(action);
+ intent.putExtra("intervalMillis", intervalMillis);
+ intent.putExtra("msg", tips);
+ intent.putExtra("id", id);
+ intent.putExtra("soundOrVibrator", soundOrVibrator);
+ PendingIntent sender = PendingIntent.getBroadcast(context, id, intent, PendingIntent
+ .FLAG_UPDATE_CURRENT);
+ ICLogger.d("intervalMillis ==> " + intervalMillis);
+ //4.4以上的
+ long targetTime = calMethod(week, calendar.getTimeInMillis());
+ ICLogger.d("targetTime ==> " + targetTime);
+ am.setExact(AlarmManager.RTC, targetTime, sender);
+ }
+
+
+ /**
+ * @param weekFlag 传入的是周几
+ * @param dateTime 传入的是时间戳(设置当天的年月日+从选择框拿来的时分秒)
+ * @return 返回起始闹钟时间的时间戳
+ */
+ private static long calMethod(int weekFlag, long dateTime) {
+ long time = 0;
+ //weekflag == 0表示是按天为周期性的时间间隔或者是一次行的,weekfalg非0时表示每周几的闹钟并以周为时间间隔
+ if (weekFlag != 0) {
+ Calendar c = Calendar.getInstance();
+ int week = c.get(Calendar.DAY_OF_WEEK);
+ if (1 == week) {
+ week = 7;
+ } else if (2 == week) {
+ week = 1;
+ } else if (3 == week) {
+ week = 2;
+ } else if (4 == week) {
+ week = 3;
+ } else if (5 == week) {
+ week = 4;
+ } else if (6 == week) {
+ week = 5;
+ } else if (7 == week) {
+ week = 6;
+ }
+ if (weekFlag == week) {
+ if (dateTime > System.currentTimeMillis()) {
+ time = dateTime;
+ } else {
+ time = dateTime + 7 * 24 * 3600 * 1000;
+ }
+ } else if (weekFlag > week) {
+ time = dateTime + (weekFlag - week) * 24 * 3600 * 1000;
+ } else {
+ time = dateTime + (weekFlag - week + 7) * 24 * 3600 * 1000;
+ }
+ } else {
+ if (dateTime > System.currentTimeMillis()) {
+ time = dateTime;
+ } else {
+ time = dateTime + 24 * 3600 * 1000;
+ }
+ }
+ return time;
+ }
+}