AlarmManager详解:android中的定时任务
来源:互联网 发布:iphone移轴摄影知乎 编辑:程序博客网 时间:2024/06/05 20:04
1.概述
AlarmManager通常用途是用来开发手机闹钟,但是AlarmManager的用处并只是这个。AlarmManager其实是一个全局定时器,它可以在指定时间或指定周期启动其他组件,在特定的时刻为我们广播一个指定的Intent。简单的说就是我们设定一个时间,当到达这个时间,就会发出广播提醒我们.
2.AlarmManager的常用方法有三个
- set(int type,long startTime,PendingIntent pi)
- setRepeating(int type,long startTime,long intervalTime,PendingIntent pi)
- setInexactRepeating(int type,long startTime,long intervalTime,PendingIntent pi)
下面我们讲一下这三个方法的用途
2.1首先了解一下 type
我们去看源码发现type就分为四种
AlarmManager.ELAPSED_REALTIME
状态值为3,
在指定的延时过后,发送广播,但不唤醒设备(闹钟在睡眠状态下不可用)。如果在系统休眠时闹钟触发,不会激活设备执行事件。AlarmManager.ELAPSED_REALTIME_WAKEUP
状态值为2,
顾名思义,在指定的延时过后,发送广播,如果设备休眠就会唤醒设备,发出广播执行事件AlarmManager.RTC
状态值为1,
该状态下闹钟使用绝对时间,也就是指定当系统调用System.currentTimeMillis()方法返回的值与triggerAtTime相等时启动operation所对应的设备(在指定的时刻,发送广播,但不唤醒设备)。如果在系统休眠时闹钟触发,它将不会被传递,直到下一次设备唤醒(闹钟在睡眠状态下不可用)。- AlarmManager.RTC_WAKEUP
状态值为0
闹钟在睡眠状态下会唤醒系统并执行提示功能,该状态下闹钟使用绝对时间,,也就是指定当系统调用System.currentTimeMillis()方法返回的值与triggerAtTime相等时启动operation所对应的设备 - AlarmManager.POWER_OFF_WAKEUP
状态值为4
表示闹钟在手机关机状态下也能正常进行提示功能,
2.2set(int type,long startTime,PendingIntent pi)设置一次性闹钟
也就是闹钟只执行一次,当执行完之后,不会再次执行
- 参数1 type: 就是上面的type
- 参数2 startTime 闹钟执行的时间
- 参数3 pi 执行的事件
2.3 setRepeating(int type,long startTime,long intervalTime,PendingIntent pi)周期性执行的定时服务
- 参数1 type: 就是上面的type
- 参数2 startTime 闹钟执行的时间
- 参数3 intervalTime 间隔时间 详情请看 2.3.1
- 参数4 pi 执行的事件
2.3.1 intervalTime的解释
AlarmManager.INTERVAL_FIFTEEN_MINUTES 间隔15分钟
AlarmManager.INTERVAL_HALF_HOUR 间隔半个小时
AlarmManager.INTERVAL_HOUR 间隔一个小时
AlarmManager.INTERVAL_HALF_DAY 间隔半天
AlarmManager.INTERVAL_DAY 间隔一天
2.4setInexactRepeating(int type,long startTime,long intervalTime,PendingIntent pi)此方法跟2.3基本上相似,只不过这个方法优化了很多,省电
3 使用步骤
- 获得AlarmManager实例
ALarmManager manager=(ALarmManager)getSystemService(ALARM_SERVICE) - 定义PendingIntent发出的广播
- 调用AlarmManager方式设置定时或者重复提醒
- 取消提醒
3.1 定义的PendingIntent
Intent intent = new Intent(AlarmTest.this, AlarmActivity.class);//AlarmActivity就是当闹钟提醒的时候打开的activity,你也可以发送广播intent.setAction("nzy");// 创建PendingIntent对象PendingIntent pi = PendingIntent.getActivity( AlarmTest.this, 0, intent, 0);Calendar calendar = Calendar.getInstance();// 根据用户选择时间来设置Calendar对象calendar.set(Calendar.HOUR, hourOfDay);calendar.set(Calendar.MINUTE, minute);// 设置AlarmManager将在Calendar对应的时间启动指定组件aManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pi);
3.2 取消闹钟
Intent intent = new Intent(AlarmTest.this, AlarmActivity.class); intent.setAction("nzy");//这里的action必须和上面设置的action一样 也就是取消的唯一标识PendingIntent pendingIntent = PendingIntent.getActivity( AlarmTest.this, 0, intent, 0); // 创建PendingIntent对象 aManager.cancel(pendingIntent);
- AlarmManager详解:android中的定时任务
- Android,定时任务AlarmManager
- 使用Android AlarmManager类定时执行任务
- Android利用AlarmManager执行定时任务
- 【AlarmManager】使用AlarmManager定时任务
- 设置定时,任务:AlarmManager
- Service+AlarmManager 定时任务
- alarmmanager定时任务
- AlarmManager 定时任务
- 定时任务,AlarmManager使用
- 定时任务,AlarmManager使用
- PendingIntent和AlarmManager--------启动定时任务及详解
- android Service重启问题,结合AlarmManager实现定时任务
- 使用AlarmManager实现Android应用每天定时执行任务
- 谈谈对Android定时任务中AlarmManager的理解
- android使用AlarmManager实现应用每天定时执行任务
- PendingIntent和AlarmManager实现定时(重复)任务 Android
- Android探索之旅(第二十三篇)AlarmManager定时任务
- Glide加载图片变形,刷新后恢复正常
- 全面升级Android面试之IntentService面试题集
- ISO 9126质量模型
- 网易笔试题-合唱团
- JavaScript 对象
- AlarmManager详解:android中的定时任务
- 用java实现邮件发送验证码
- Unable to fetch namespace information from active NN at hadoop042/10.1.2.42:8020: Call From hadoop0
- 将excel中的数据导入hive
- Java 8种基本数据类型
- ES6 函数参数的默认值
- 深入了解JAVA可变长度的参数(Varargs)
- Pycharm中进行Python远程开发
- 101. Symmetric Tree