使用AlarmManager简单实现定时每天或重复执行任务,有3种方式
1)AlarmManager+Service 实现方式
2)AlarmManager+InterService 实现方式
3)AlarmManger + BoardcastReceiver 实现方式
需要在AndroidMainifast.xml 增加权限
1)增加ALARM(闹钟)权限
<uses-permission android:name="android.permission.SET_ALARM"/><uses-permission android:name="android.permission.WAKE_LOCK"/><uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
2)增加Service和BoardcastReceiver注册
<service android:name=".service.AlarmService" /><service android:name=".service.AlarmIntentService"/><receiver android:name=".broadcast.AlarmBroadcastReceiver" />
3)Service和BoardcastReceiver定义
3.1)AlarmService
public class AlarmService extends Service {private final String TAG = "AlarmService";@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {// 在这里执行你的任务Log.e(TAG,"调用闹钟,调用时间:"+ DateHelper.timestampToDate(System.currentTimeMillis()) );// 任务执行完毕后,停止Service stopSelf();return START_NOT_STICKY;}@Overridepublic IBinder onBind(Intent intent) {return null;} }
3.2)AlarmIntentService
public class AlarmIntentService extends IntentService {private final String TAG = "AlarmIntentService";public AlarmIntentService(){super("AlarmIntentService");}@Overrideprotected void onHandleIntent(Intent intent){Log.e(TAG,"调用闹钟,调用时间:"+ DateHelper.timestampToDate(System.currentTimeMillis()) );} }
3.3)AlarmBoardcastReceiver
public class AlarmBroadcastReceiver extends BroadcastReceiver {private final String TAG = "AlarmBroadcastReceiver";public static String alarmboradcastAction = "alarm.broadcast.upload";@Overridepublic void onReceive(Context context, Intent intent){if(intent.getAction().equals(alarmboradcastAction)){Log.e(TAG,"调用闹钟,调用时间:"+ DateHelper.timestampToDate(System.currentTimeMillis()));}} }
4)AlarmManager结合Service、IntentService、boardcastReceiver使用
4.1)AlarmManager+Service 使用
/*** AlarmManager + Service 实现定时任务* */public void AlarmServiceTest(){// 设置定时任务的代码片段Intent alarmIntent = new Intent(this, AlarmService.class);PendingIntent pendingIntent = PendingIntent.getService(this, 0, alarmIntent, 0);AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);// 设置单次闹钟或重复闹钟// 设置为单次闹钟long triggerAtMillis = System.currentTimeMillis() + 1000; // 10秒后触发alarmManager.set(AlarmManager.RTC_WAKEUP, triggerAtMillis, pendingIntent);// 或者设置重复闹钟(最小时间间隔:1 min)//long intervalMillis = 1000 * 60*1; // 每隔60秒触发一次//alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), intervalMillis, pendingIntent);}
4.2)AlarmManager+IntentService 使用
/*** AlarmManager + IntentService 实现定时任务* */public void AlarmInterServiceTest(){// 设置定时任务的代码片段Intent alarmIntent = new Intent(this, AlarmIntentService.class);PendingIntent pendingIntent = PendingIntent.getService(this, 0, alarmIntent, 0);AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);// 设置单次闹钟或重复闹钟// 设置为单次闹钟//long triggerAtMillis = System.currentTimeMillis() + 1000; // 10秒后触发//alarmManager.set(AlarmManager.RTC_WAKEUP, triggerAtMillis, pendingIntent);// 或者设置重复闹钟(最小时间间隔:1 min)long intervalMillis = 1000 * 60*1; // 每隔60秒触发一次 alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), intervalMillis, pendingIntent);}
4.3)AlarmManager+BroadcastReceiver 使用
/*** AlarmManager+BroadcastReceiver 方式实现定时任务* */public void AlarmBroadcastTest(){// 设置定时任务的代码片段Intent alarmIntent = new Intent(this, AlarmBroadcastReceiver.class);alarmIntent.setAction(AlarmBroadcastReceiver.alarmboradcastAction);PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, 0);AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);// 设置单次闹钟或重复闹钟// 设置为单次闹钟//long triggerAtMillis = System.currentTimeMillis() + 1000; // 10秒后触发//alarmManager.set(AlarmManager.RTC_WAKEUP, triggerAtMillis, pendingIntent);// 或者设置重复闹钟long intervalMillis = 1000 * 60*1; // 每隔60秒触发一次1000 * 60 alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), intervalMillis, pendingIntent);}
5)AlarmManager定时的调用
@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);AlarmBroadcastTest();AlarmServiceTest();AlarmInterServiceTest();}
6)执行调用效果
E/AlarmBroadcastReceiver: 调用闹钟,调用时间:2025-01-15 15:48:37 E/AlarmService: 调用闹钟,调用时间:2025-01-15 15:48:37 E/AlarmIntentService: 调用闹钟,调用时间:2025-01-15 15:48:37 E/AlarmBroadcastReceiver: 调用闹钟,调用时间:2025-01-15 15:49:32 E/AlarmIntentService: 调用闹钟,调用时间:2025-01-15 15:49:32 E/AlarmBroadcastReceiver: 调用闹钟,调用时间:2025-01-15 15:50:32
参考网站:https://blog.51cto.com/u_16099281/12593446