我尝试这个脚本:
var myConstraints = Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
.build()
val uploadInventorySyncWork = OneTimeWorkRequest.Builder(UploadInventorySyncWorker::class.java)
.setConstraints(myConstraints)
.addTag(UploadInventoryModel.ONE_TIME_TAG)
.build()
WorkManager.getInstance().enqueue(uploadInventorySyncWork)
WorkManager.getInstance().getWorkInfosByTagLiveData(UploadInventoryModel.ONE_TIME_TAG)
.observeForever { workStatus ->
if (workStatus != null) {
attempt+1
for (works in workStatus) {
Log.d("dodol34 no", attempt.toString() + " | " + works.state.toString())
if (works.state == WorkInfo.State.SUCCEEDED) {
}
}
}
}
它在android oreo中工作正常。但在android牛轧糖中它始终处于ENQUEUED状态。这是logcat结果(android牛轧糖):
12-02 00:20:13.890 23118-23118/com.mockie.daikokuten D/dodol34 no: 1 | ENQUEUED
1 | ENQUEUED
1 | ENQUEUED
1 | ENQUEUED
1 | ENQUEUED
1 | ENQUEUED
1 | ENQUEUED
1 | ENQUEUED
完全没有错误,有时可以,有时不能。