android AlarmManager之定时任务

小豆苗 4月前 ⋅ 355 阅读
  1. 仅执行一次
    1. AlarmManager alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
              Intent intent = new Intent(context, AlarmReceiver.class);
              intent.setAction(Const.ACTION_ALARM_POLICY_SCHEDULE);
              intent.putExtra("start_time", startTime);
              intent.putExtra("end_time", endTime);
              intent.putExtra("start_week", startWeek);
              intent.putExtra("end_week", endWeek);
              intent.putExtra("time_differ", timeDiffer);
              PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_IMMUTABLE);
      
              Calendar calendar = Calendar.getInstance(Locale.CHINA);
              calendar.setTimeZone(TimeZone.getTimeZone("GMT+8"));
              calendar.setTimeInMillis(System.currentTimeMillis());
              Logger.d("====当前几点=="+calendar.get(Calendar.HOUR_OF_DAY)+" : "+calendar.get(Calendar.MINUTE));
              calendar.set(Calendar.HOUR_OF_DAY, timeArray[0]);
              calendar.set(Calendar.MINUTE, timeArray[1]);
              calendar.set(Calendar.SECOND, timeArray[2]);
      
              // 设置执行一次
              alarmMgr.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), alarmIntent);
  2. 每天执行一次
    1. AlarmManager alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
              Intent intent = new Intent(context, AlarmReceiver.class);
              intent.setAction("com.wsmee.app.alarm.action.18Clock");
              PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_IMMUTABLE);
      
              Calendar calendar = Calendar.getInstance(Locale.CHINA);
              calendar.setTimeZone(TimeZone.getTimeZone("GMT+8"));
              calendar.setTimeInMillis(System.currentTimeMillis());
              calendar.set(Calendar.HOUR_OF_DAY, 00);
              calendar.set(Calendar.MINUTE, 15);
      
              // 设置每隔一天执行一次
              alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                      AlarmManager.INTERVAL_DAY, alarmIntent);
  3. 设置在上午8:30 执行,每隔指定时间执行一次
    1. AlarmManager alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
              Intent intent = new Intent(context, AlarmReceiver.class);
              PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
      
              // 设置闹钟在上午8:30 执行
              Calendar calendar = Calendar.getInstance();
              calendar.setTimeInMillis(System.currentTimeMillis());
              calendar.set(Calendar.HOUR_OF_DAY, 8);
              calendar.set(Calendar.MINUTE, 30);
      
              // 设置闹钟每隔20分钟触发一次
              alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                      1000 * 60 * 20, alarmIntent);
  4. 指定时间启动服务
    1. AlarmManager alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
      
              Intent intent = new Intent(context, TaskService.class);
              PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 
                      PendingIntent.FLAG_UPDATE_CURRENT| PendingIntent.FLAG_IMMUTABLE);
              
              Calendar calendar = Calendar.getInstance();
              calendar.set(Calendar.HOUR_OF_DAY, 9);
              calendar.set(Calendar.MINUTE, 00);
              calendar.set(Calendar.SECOND, 00);
      
              //上午9点开启服务
              // 6.0及以上
           alarmMgr.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
  5.  

全部评论: 0

    我有话说: