我想要一个需要互联网连接的任务,并且必须每天安排一次。我使用警报管理器部分完成上述任务,如下所示。
Calendar c = Calendar.getInstance(); //gives u calendar with current time
c.add(Calendar.SECOND, 30);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 1, intent, 0);
if (alarmManager != null) {
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pendingIntent);
}
// my broadcast receiver
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//my task will be here
// for now I planned to check internet here and if it has i'll continue the task
}
}
//in manifest
<receiver android:name=".broadcast_receiver.AlarmReceiver" />
有没有办法在有互联网连接的同一天推迟上述任务?
AlarmManager
可以用来达到上述要求吗?
您可以使用WorkManager来实现这一点。查看官方文档以获取更多信息。