我正在使用WorkManager来安排一些任务,但问题是在单个调用中,WorkManager多次执行这些任务(doWork())。
我正在使用:
'android.arch.work:work-runtime:1.0.0-alpha08'
我尝试使用了-alpha07,06,05,04。但是我遇到了同样的问题。有时它甚至会一次执行5-6次。
以下是代码:
public class MyWorker extends Worker {
@NonNull
@Override
public Result doWork() {
Log.i("CountWorker","0");
sendNotification("Notice", "A notice was sent");
return Result.SUCCESS;
}
这是一个Activity(活动)
public class MyWorkerActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final PeriodicWorkRequest pwr = new PeriodicWorkRequest
.Builder(MyWorker.class, 16, TimeUnit.MINUTES)
.setConstraints(Constraints.NONE)
.build();
WorkManager.getInstance().enqueue(pwr);
}
}
这是来自Logcat的结果:
09-24 16:44:35.954 22779-22816/com.simran.powermanagement I/CountWorker: 0
09-24 16:44:35.970 22779-22817/com.simran.powermanagement I/CountWorker: 0
09-24 16:44:35.977 22779-22818/com.simran.powermanagement I/CountWorker: 0