AlarmManagerUtils.setAlarmOpen(context, 1, Integer.valueOf(a1), Integer.valueOf(a2), 0, 1, 0, "提醒开启风扇时间到了");
AlarmManagerUtils.setAlarmClose(context, 1, Integer.valueOf(b1), Integer.valueOf(b2), 0, 2, 0, "提醒关闭风扇时间到了");
/**
* 定时提醒工具类
*/
public class AlarmManagerUtils {
public static final String ID = "ID";
public static final String INTERVAL_MILLIS = "INTERVAL_MILLIS";
public static final String TIPS = "TIPS";
/**
* 设置定时提醒,供AlarmOpenService使用
*/
public static void setAlarmOpenTime(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);
PendingIntent sender = PendingIntent.getService(context, intent.getIntExtra(ID, 0), intent, PendingIntent.FLAG_CANCEL_CURRENT);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
am.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, timeInMillis, sender);
else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
AlarmManager.AlarmClockInfo alarmClockInfo = new AlarmManager.AlarmClockInfo(timeInMillis, sender);
am.setAlarmClock(alarmClockInfo, sender);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
am.setExact(AlarmManager.RTC_WAKEUP, timeInMillis, sender);
}
/**
* 设置定时提醒,供AlarmCloseService使用
*/
public static void setAlarmCloseTime(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);
PendingIntent sender = PendingIntent.getService(context, intent.getIntExtra(ID, 0), intent, PendingIntent.FLAG_CANCEL_CURRENT);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
am.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, timeInMillis, sender);
else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
AlarmManager.AlarmClockInfo alarmClockInfo = new AlarmManager.AlarmClockInfo(timeInMillis, sender);
am.setAlarmClock(alarmClockInfo, sender);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
am.setExact(AlarmManager.RTC_WAKEUP, timeInMillis, sender);
}
/**
* @param flag 周期性时间间隔的标志,flag = 0 表示一次性的闹钟, flag = 1 表示每天提醒的闹钟(1天的时间间隔),flag = 2 表示按周每周提醒的闹钟(一周的周期性时间间隔)
* @param hour 时
* @param minute 分
* @param second 秒
* @param id 闹钟的id
* @param week week=0表示一次性闹钟或者按天的周期性闹钟,非0 的情况下是几就代表以周为周期性的周几的闹钟
* @param tips 闹钟提示信息
* // * @param soundOrVibrator 0表示只有震动提醒 1表示只有铃声提醒 2表示声音和震动都执行
*/
@SuppressLint("ShortAlarm")
public static void setAlarmOpen(Context context, int flag, int hour, int minute, int second, int id, int week, String tips) {
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
long intervalMillis = 0;
calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), hour, minute, second);
if (flag == 0)
intervalMillis = 0;
else if (flag == 1)
intervalMillis = AlarmManager.INTERVAL_DAY;
else if (flag == 2)
intervalMillis = AlarmManager.INTERVAL_DAY * 7;
Intent intent = new Intent(AlarmOpenService.ACTION);
intent.putExtra(ID, id);
intent.putExtra(TIPS, tips);
intent.putExtra(INTERVAL_MILLIS, intervalMillis);
PendingIntent sender = PendingIntent.getService(context, id, intent, PendingIntent.FLAG_CANCEL_CURRENT);
long time = calMethod(week, calendar.getTimeInMillis());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
am.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, time, sender);
else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
AlarmManager.AlarmClockInfo alarmClockInfo = new AlarmManager.AlarmClockInfo(time, sender);
am.setAlarmClock(alarmClockInfo, sender);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
am.setExact(AlarmManager.RTC_WAKEUP, time, sender);
else
am.setRepeating(AlarmManager.RTC_WAKEUP, time, intervalMillis, sender);// 可能存在不精确的问题
}
/**
* @param flag 周期性时间间隔的标志,flag = 0 表示一次性的闹钟, flag = 1 表示每天提醒的闹钟(1天的时间间隔),flag = 2 表示按周每周提醒的闹钟(一周的周期性时间间隔)
* @param hour 时
* @param minute 分
* @param second 秒
* @param id 闹钟的id
* @param week week=0表示一次性闹钟或者按天的周期性闹钟,非0 的情况下是几就代表以周为周期性的周几的闹钟
* @param tips 闹钟提示信息
* // * @param soundOrVibrator 0表示只有震动提醒 1表示只有铃声提醒 2表示声音和震动都执行
*/
@SuppressLint("ShortAlarm")
public static void setAlarmClose(Context context, int flag, int hour, int minute, int second, int id, int week, String tips) {
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
long intervalMillis = 0;
calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), hour, minute, second);
if (flag == 0)
intervalMillis = 0;
else if (flag == 1)
intervalMillis = AlarmManager.INTERVAL_DAY;
else if (flag == 2)
intervalMillis = AlarmManager.INTERVAL_DAY * 7;
Intent intent = new Intent(AlarmCloseService.ACTION);
intent.putExtra(ID, id);
intent.putExtra(TIPS, tips);
intent.putExtra(INTERVAL_MILLIS, intervalMillis);
PendingIntent sender = PendingIntent.getService(context, id, intent, PendingIntent.FLAG_CANCEL_CURRENT);
long time = calMethod(week, calendar.getTimeInMillis());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
am.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, time, sender);
else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
AlarmManager.AlarmClockInfo alarmClockInfo = new AlarmManager.AlarmClockInfo(time, sender);
am.setAlarmClock(alarmClockInfo, sender);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
am.setExact(AlarmManager.RTC_WAKEUP, time, sender);
else
am.setRepeating(AlarmManager.RTC_WAKEUP, time, intervalMillis, 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);
PendingIntent pi = PendingIntent.getService(context, id, intent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
am.cancel(pi);
}
/**
* triggerAtMillis 计算方法
*
* @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;
}
}
public class AlarmOpenService extends Service {public static final String ACTION = "org.tcshare.app.alarmopen";private static final String TAG = "aaaaa";private Boolean onOff = true;private Thread openThread;@Overridepublic IBinder onBind(Intent intent) {return null;}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {Context context = getApplicationContext();openThread = new Thread() {@Overridepublic void run() {long intervalMillis = intent.getLongExtra(AlarmManagerUtils.INTERVAL_MILLIS, 0);if (intervalMillis != 0) {AlarmManagerUtils.setAlarmOpenTime(context, System.currentTimeMillis() + intervalMillis, intent);Log.e(TAG, "开启业务 ");}}}};openThread.start();return super.onStartCommand(intent, flags, startId);} }