我想在应用程序关闭时定期运行一些任务。它在 Android API26 上运行良好,但在 Android API28 上运行良好。我正在使用 WorkManager 2.0.1。
MainActivity.java
PeriodicWorkRequest refreshWork;
refreshWork = new PeriodicWorkRequest.Builder(MyWorker.class, 16, TimeUnit.MINUTES)
.setConstraints(Constraints.NONE)
.addTag("MyTag")
.build();
WorkManager.getInstance().enqueueUniquePeriodicWork("MyWorker", ExistingPeriodicWorkPolicy.KEEP ,refreshWork);
MyWorker.java
public Result doWork() {
Log.i("MyLog","I am here");
return Worker.Result.success();
}