Android定时任务之Service + AlarmManger + BroadcastReceiver
—— 这个世界,一百万人里面只有一个主角。当主角的都是能够达到极致的人。
AlarmManger
闹钟服务,在特定的时刻为我们广播一个指定的Intent,简单说就是我们自己定一个时间, 然后当到时间时,AlarmManager会为我们广播一个我们设定好的Intent,比如时间到了,可以指向某个Activity或者Service。另外要注意一点的是,AlarmManager主要是用来在某个时刻运行你的代码的,即时你的APP在那个特定时间并没有运行!还有,从API 19开始,Alarm的机制都是非准确传递的,操作系统将会转换闹钟 ,来最小化唤醒和电池的使用!某些新的API会支持严格准确的传递,见 setWindow(int, long, long, PendingIntent)和setExact(int, long, PendingIntent)。 targetSdkVersion在API 19之前应用仍将继续使用以前的行为,所有的闹钟在要求准确传递的情况下都会准确传递。
设置闹钟
- 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): 重复性闹钟,时间不固定
- setAndAllowWhileIdle(int type, long triggerAtMillis, PendingIntent operation): 和set方法类似,这个闹钟运行在系统处于低电模式时有效
- setExactAndAllowWhileIdle(int type, long triggerAtMillis, PendingIntent operation):和setAndAllowWhileIdle一样,如果需要在低功耗模式下触发闹钟,那可以使用这个方法。
- setExact(int type, long triggerAtMillis, PendingIntent operation): 在规定的时间精确的执行闹钟,比set方法设置的精度更高,建议使用这个方法设置闹钟
setInexactRepeating和setRepeating两种方法都是用来设置重复闹铃的,setRepeating执行时间更为精准。在Android 4.4之后,Android系统为了省电把时间相近的闹铃打包到一起进行批量处理,这就使得setRepeating方法设置的闹铃不能被精确的执行,必须要使用setExact来代替。但是手机进入低电耗模式之后,这个时候通过setExact设置的闹铃也不是100%准确了,需要用setExactAndAllowWhileIdle方法来设置,闹铃才能在低电耗模式下被执行。
上面方法中的参数说明:
type:闹钟类型,有五种类型:
AlarmManager.ELAPSED_REALTIME: 闹钟在手机睡眠状态下不可用,该状态下闹钟使用相对时间(相对于系统启动开始),状态值为3 AlarmManager.ELAPSED_REALTIME_WAKEUP:闹钟在睡眠状态下会唤醒系统并执行提示功能,该状态下闹钟也使用相对时间,状态值为2 AlarmManager.RTC:闹钟在睡眠状态下不可用,该状态下闹钟使用绝对时间,即当前系统时间,状态值为1 AlarmManager.RTC_WAKEUP:表示闹钟在睡眠状态下会唤醒系统并执行提示功能,该状态下闹钟使用绝对时间,状态值为0 AlarmManager.POWER_OFF_WAKEUP:表示闹钟在手机关机状态下也能正常进行提示功能,所以是5个状态中用的最多的状态之一,该状态下闹钟也是用绝对时间,状态值为4;不过本状态好像受SDK版本影响,某些版本并不支持;
startTime:闹钟的第一次执行时间,以毫秒为单位,可以自定义时间,不过一般使用当前时间。 需要注意的是:本属性与第一个属性(type)密切相关, 如果第一个参数对应的闹钟使用的是相对时间 (ELAPSED_REALTIME和ELAPSED_REALTIME_WAKEUP),那么本属性就得使用相对时间 (相对于系统启动时间来说),比如当前时间就表示为:SystemClock.elapsedRealtime(); 如果第一个参数对应的闹钟使用的是绝对时间(RTC、RTC_WAKEUP、POWER_OFF_WAKEUP), 那么本属性就得使用绝对时间,比如当前时间就表示为:System.currentTimeMillis()。
intervalTime:表示两次闹钟执行的间隔时间,也是以毫秒为单位。
PendingIntent:绑定了闹钟的执行动作,比如发送一个广播、给出提示等等。不过需要注意:
- 如果是通过启动服务来实现闹钟提示的话,PendingIntent对象的获取就应该采用Pending.getService (Context c,int i,Intent intent,int j)方法;
- 如果是通过广播来实现闹钟提示的话,PendingIntent对象的获取就应该采用PendingIntent.getBroadcast (Context c,int i,Intent intent,int j)方法;
- 如果是采用Activity的方式来实现闹钟提示的话,PendingIntent对象的获取就应该采用 PendingIntent.getActivity(Context c,int i,Intent intent,int j)方法;
如果这三种方法错用了的话,虽然不会报错,但是看不到闹钟提示效果。
triggerAtMillis:闹钟触发时间,与startTime一样,也是以毫秒为单位,跟第一个属性(type)密切相关。
取消闹钟
//如果是PendingIntent方式注册的闹铃。 cancel(PendingIntent pi) //如果是AlarmManager.OnAlarmListener方式注册的闹铃 cancel(AlarmManager.OnAlarmListener listener)
Service + AlarmManger + BroadcastReceiver
上面方式结合可以实现精确定时,适用于配合service在后台执行一些长期的定时行为,比如每隔五分钟保存一次历史数据。
public class HistoryDataService extends Service { private static final String ALARM_ACTION = "SAVE_HISTORY_DATA_ACTION"; private static final int TIME_INTERVAL = 300000; // 5min 1000 * 60 * 5 private PendingIntent pendingIntent; private AlarmManager alarmManager; private BroadcastReceiver alarmReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { //定时任务 //todo Log.i("HistoryDataService", "onReceive: 执行保存历史数据"); //如果版本高于4.4,需要重新设置闹钟 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { alarmManager.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + TIME_INTERVAL, pendingIntent); } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { alarmManager.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + TIME_INTERVAL, pendingIntent); } } }; public HistoryDataService() { } @Override public void onCreate() { super.onCreate(); IntentFilter intentFilter = new IntentFilter(ALARM_ACTION); registerReceiver(alarmReceiver, intentFilter); alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); pendingIntent = PendingIntent.getBroadcast(this, 0, new Intent(ALARM_ACTION), 0); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) else } @Override public void onDestroy() { super.onDestroy(); unregisterReceiver(alarmReceiver); alarmManager.cancel(pendingIntent); } @Override public IBinder onBind(Intent intent) { // TODO: Return the communication channel to the service. throw new UnsupportedOperationException("Not yet implemented"); } }
------转载请注明出处,感谢您对原创作者的支持------
有偿提供技术支持、Bug修复、项目外包、毕业设计、大小作业
Android学习小站
Q Q:1095817610
微信:jx-helu
邮箱:1095817610@qq.com
添加请备注"Android学习小站"
