AlarmManager定时开关业务
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);} }
AlarmManager定时开关业务相关推荐
- .NET应用如何优雅的实现功能定时开关
点击上方蓝字关注"汪宇杰博客" 导语 我们在打工的时候,总能遇到一种类型的需求:"我想要这个活动广告在双11期间才显示","我想要这个API在20号以 ...
- Arduino项目——定时控制,手机控制Arduino实现远程开关和定时开关
功能效果 1. 通过手机app的按键实时控制舵机转动. ...
- 腾达f3虚拟服务器设置,高手指南win7系统腾达F3路由器设置WIFI定时开关的法子
许多win7系统用户在工作中经常需要对win7系统腾达F3路由器设置WIFI定时开关进行设置,比如近日有用户到本站反映说win7系统腾达F3路由器设置WIFI定时开关的问题,但是却不知道要怎么设置wi ...
- 【锐捷无线】定时开关无线信号配置
功能介绍 定时开关无线功能适用于那些在固定周期提供无线接入的场合.比如:某高校的教学楼只在白天上课时提供无线接入服务:某办公楼留给访客的无线网络只在工作日的上班时间开启等等.此功能可以减少网络流量,节 ...
- openwrt折腾记1-DDNS动态域名更新与WIFI定时开关脚本
本来无事,因为刚得知ipv6可以直接访问内部家庭宽带,于是开始试用.移动的第三方公司,运维人员给的答复是IP公网的需要安装专线,ipv6够呛. 0x01使用ipv6拨号 首先通过他的超极用户进入光猫, ...
- TP-LINK 720N刷了OpenWRT后LED灯定时开关
TP-LINK 720N刷了OpenWRT后LED灯定时开关 参考链接:https://blog.wangmao.me/openwrt-timing-off-led-lights.html 因为家里有 ...
- PLC如何实现循环定时开关(Cycle_time_switch)
1.smartPLC 如何实现循环定时开关子程序,我们这里讨论的是实现这样的子程序,可以多次调用,控制多个回路的循环定时开关. 2.下面直接上代码: SUBROUTINE_BLOCK SecCycli ...
- H3C NX30 PRO无线路由器刷OpenWRT后LED定时开关
H3C NX30 PRO无线路由器刷OpenWRT后LED定时开关 前几天刚入手的H3C的NX30 PRO路由器做旁路由使用,跟着b站up主@酱紫表的视频[H3C NX30 Pro 超值百元路由器 ...
- 广告牌定时器怎么设置时间_定时开关如何设置时间呢
路灯定时开关怎么设置定时 内容如下:1.先按取消键5秒就可解锁设置2.如果有设置,先选择,然后选择校时,校分,这个是开:3.再次按设置,校时,校分,第一组就设置完成.以此类推下去就行.4.选择自动,或 ...
最新文章
- “物联网+云平台”的实验室管理方案,瞄准的是生物医药和化工行业
- Python的小特别
- centos7 搭建Docker Registry
- 关于vue 框架与后台框架的混合使用的尝试
- Windows/Android/iOS 等常见 User-Agent 大全
- 如何在word(非wps)里面插入公式和编号(完美格式)
- 快速入门spring data jpa 2,多表操作,逻辑删除
- HTML5CSS3:Day03 2D动画 3D动画 CSS3过渡
- 两只PNP晶体三极管和四个电阻组成恒流源电路
- 	定义一个变量,是一个三位数,求各个位数的和
- Python的并发并行[1] - 线程[3] - 多线程的同步控制
- jQuery-入口函数
- 火焰检测 python
- 第七章Linux 系统——存储管理高级课程
- PWM 调光的线性降压 LED 恒流驱动器 OC7130B
- 渝北统景碑口规划开发_统景风景区旅游镇总体规划(2011—2030)说明书
- 解决(您可能是盗版软件的受害者)
- 用python监控女朋友的网站看你女朋友每天都在看一些什么东西
- 【pytest】编写、共享及使用 fixture(测试夹具)
- 重磅 | 华为云WeLink 设计体验再升级!
热门文章
- Klipper使用I2C接口OLED屏(MKS Gen-L V2.1为例)
- pygame实现弹球游戏
- 基于linux7的ectd安装与部署
- apr各个版本工具包下载及安装教程
- 知识农场-开发过程记录
- 地球最强安卓模拟器BlueStacks蓝叠全版本ROOT支持最新4.x
- 2018 香港大学面试经历(Msc in Computer Science) 上香还愿
- JS正则表达式学习总结 +多个校验案例
- 基于Java毕业设计超市货品进销存系统后台源码+系统+mysql+lw文档+部署软件
- 光模块核心器件主要有哪些?