android-workstatus观察器始终处于排队状态
作者:互联网
我正在尝试观察我的工人,但他们始终处于排队状态,或者有时它正在运行,但从未成功或失败.
是从doWork()返回的workStatus.state还是不同?
这是我的工作者脚本:
package com.mockie.daikokuten.sync.workers
import androidx.work.Worker
class TestWorker:Worker()
{
override fun doWork():Worker.Result
{
return Worker.Result.SUCCESS
}
}
这是观察工人的脚本:
val test = PeriodicWorkRequest.Builder(
TestWorker::class.java,
PeriodicWorkRequest.MIN_PERIODIC_INTERVAL_MILLIS,
TimeUnit.MILLISECONDS)
.addTag("test_worker")
.build()
WorkManager.getInstance()?.enqueueUniquePeriodicWork("test_worker", ExistingPeriodicWorkPolicy.KEEP, test)
WorkManager.getInstance()!!.getStatusesByTag("test_worker")
.observe(this, Observer { workStatus ->
if (workStatus != null)
{
for(ws in workStatus)
{
Log.d(":dump2 id ", ws.id.toString())
Log.d(":dump2 tag", ws.tags.toString())
Log.d(":dump2 state", ws.state.toString())
}
}
})
这是Logcat中的结果:
07-23 17:12:30.901 29740-29740/com.mockie.daikokuten D/:dump2 id: 5c6297f7-11d8-4f2f-a327-773672a7435c
07-23 17:12:30.901 29740-29740/com.mockie.daikokuten D/:dump2 tag: [test_worker, com.mockie.daikokuten.sync.workers.TestWorker]
07-23 17:12:30.901 29740-29740/com.mockie.daikokuten D/:dump2 state: ENQUEUED
解决方法:
对于您的定期工作要求,您应该看到
ENQUEUED - RUNNING - ENQUEUED
后面的ENQUEUED是下一个工作请求的状态.
在运行和排队之间,您可能会很快获得成功,但是我从未见过.
对于一次性工作请求,您会看到
ENQUEUED - RUNNING - SUCCEEDED
或您在doWork()中返回的任何内容.
(Android 8.1 API 27,1.0.0-alpha04)
标签:android,android-studio,kotlin,android-workmanager 来源: https://codeday.me/bug/20191011/1889836.html