我想每天早上6点每天运营一份工作服务并做一些工作。因为我只能设置setPeriod
我无法找到每天早上6点执行或重新安排任务的方法。提前谢谢。
private static long periodSecs = 7200L; //TODO: Set 6AM everyday
private static final String JOB_TAG = "NOTIFICATION_JOB";
private void scheduleJob() {
Timber.i("scheduleJob");
Task task = new PeriodicTask.Builder()
.setService(Job.class)
.setPeriod(periodSecs)
.setTag(JOB_TAG)
.setRequiredNetwork(Task.NETWORK_STATE_CONNECTED)
.setPersisted(true)
.setUpdateCurrent(true)
.build();
GcmNetworkManager.getInstance(this).schedule(task);
}
答案 0 :(得分:0)
AlaraManager会定期为您执行任务。 像这样使用它。
public void scheduleAlarm(View v)
{
//set time here
Long time = new GregorianCalendar().getTimeInMillis()+24*60*60*1000;
// Create an Intent and set the class that will execute when the Alarm triggers.
Intent intentAlarm = new Intent(this, AlarmReceiver.class);
// onReceive() method of this class will execute when the broadcast from your alarm is received.
// Get the Alarm Service
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
// Set the alarm for a particular time.
alarmManager.set(AlarmManager.RTC_WAKEUP, time, PendingIntent.getBroadcast(this, 1, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT));
Toast.makeText(this, "Alarm Scheduled for Tommrrow", Toast.LENGTH_LONG).show();
}
AlarmReceiver类
public class AlarmReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
//perform what ever the operation you want
//when alarm triggers daily on your time
}
}