如何在Android中使用警报管理器重复执行任务

时间:2018-06-22 04:35:54

标签: java android service

我想在特定时间重复任务,例如9:45、10:10

File->Settings->Build,Execution,Deployment->Gradle-> Uncheck Offline Work

1 个答案:

答案 0 :(得分:0)

尝试一下:-

private AlarmManager alarmMgr;
private PendingIntent alarmIntent;
...
alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiver.class);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

// Set the alarm to start at 8:30 a.m.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 8);
calendar.set(Calendar.MINUTE, 30);

// setRepeating() lets you specify a precise custom interval--in this case,
// 20 minutes.
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
        1000 * 60 * 20, alarmIntent);

参考文献:-https://developer.android.com/training/scheduling/alarms