问题描述
似乎ListenableWorker不再具有@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)限制,但是我找不到或找不到有关如何正确返回ListenableFuture的示例.结果>在我覆盖的startWork()函数中.
It seems ListenableWorker no longer has the @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) restriction however I can't figure out or find examples on how to properly return a ListenableFuture< Result> in my overriden startWork() function.
据我所知,唯一的选择是返回SettableFuture.create< Result>(),但这仍然需要禁止显示"RestrictedApi"警告/错误.
As far as I can tell, the only option is to return a SettableFuture.create< Result>() but that still requires suppressing the "RestrictedApi" warning/error.
有人知道更简单的方法吗?
Anyone know a simpler way ?
据我所知,这是使用CallbackToFutureAdapter.Completer
This is the way to do it as far as I've gathered which makes use of CallbackToFutureAdapter.Completer
override fun startWork(): ListenableFuture<Result> {
return CallbackToFutureAdapter.getFuture({
it.set(ListenableWorker.Result.success())
})
}
推荐答案
- 如果您使用番石榴,请使用
ListeningExecutorService
. - 否则,请在gradle文件中包含
councurrent-futures
使用CallbackToFutureAdapter
.
- If you use Guava, use
ListeningExecutorService
. - Otherwise, include
councurrent-futures
in your gradle file and useCallbackToFutureAdapter
.
这篇关于如何返回ListenableFuture< Result>与工作管理器2.0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!