安排仅在有Internet连接的情况下每天运行一次的任务

问题描述 投票:-1回答:1

我想执行一项需要互联网连接的任务,并且必须每天安排一次。我部分使用警报管理器来完成上述任务,如下所示。

 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是否可以用来达到上述要求?

java android alarmmanager repeatingalarm
1个回答
0
投票

您可以使用WorkManager来实现。查看the official docs了解更多信息。

© www.soinside.com 2019 - 2024. All rights reserved.