Android:通过异步 Worker 类更改 TextView
我试图通过使用 WorkManager 和 Worker 类在 android 中尝试异步编程,方法是让工作人员更改 MainActivity 的视图(而不是在 MainActivity 本身上执行此操作) )。这是我的 MainActivity 代码:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val myWorkRequest: WorkRequest =
OneTimeWorkRequestBuilder<myWorker>()
.build()
WorkManager
.getInstance(this)
.enqueue(myWorkRequest)
}
这是我的 Worker 类的代码:
class myWorker(appContext: Context, workerParams: WorkerParameters, mainView: View): Worker(appContext, workerParams) {
private val ctx = appContext
private val view = mainView
override fun doWork(): Result {
// Do the work here
myWork()
// Indicate whether the work finished successfully with the Result
return Result.success()
}
fun myWork(): Boolean{
val textView2: TextView = view.findViewById(R.id.textView2)
textView2.setText("TEST!")
return true;
}
}
我很确定问题在于将参数传递到 myWorker 中,而且我不太明白如何以及到底是什么我应该传递给 Worker 类以使其影响 MainActivity。
I am trying to attempt async programming in android by using the WorkManager and Worker classes by getting a worker to change the view of the MainActivity (instead of doing it on the MainActivity itself). Here is my code for MainActivity:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val myWorkRequest: WorkRequest =
OneTimeWorkRequestBuilder<myWorker>()
.build()
WorkManager
.getInstance(this)
.enqueue(myWorkRequest)
}
And this is the code for my Worker class:
class myWorker(appContext: Context, workerParams: WorkerParameters, mainView: View): Worker(appContext, workerParams) {
private val ctx = appContext
private val view = mainView
override fun doWork(): Result {
// Do the work here
myWork()
// Indicate whether the work finished successfully with the Result
return Result.success()
}
fun myWork(): Boolean{
val textView2: TextView = view.findViewById(R.id.textView2)
textView2.setText("TEST!")
return true;
}
}
I am pretty sure the problem lies in the passing of parameters into myWorker, and I don't quite understand how and what exactly I should be passing into the Worker class in order to make it affect the MainActivity.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您没有将
View
传递给 Worker Class 。绝不! 。这可能会导致某些问题,例如 NPE 和内存泄漏。现在来讨论第二个问题,通常情况下,您不使用
WorkManager
进行异步编程。WorkManager
更适合计划任务,您将来想做的事情,并且可能会被推迟。这通常被称为后台任务(基本上是在应用程序未打开时)。看看这个和此线程 。如果您想要执行一些异步操作,您基本上有两个广泛使用的选项
Coroutines
和Rxjava
。由于您已经在使用kotlin
,您可以使用Coroutines
,而且它很容易实现。现在,如果您想要从
Worker
向 UI 组件提供回调,您可以按照 这个。通常,如果您的异步任务是即时任务,则您不会使用 Worker,在这种情况下您会使用 Thread。 Coroutines 是一个多线程框架,所以你可以使用它。
You do not pass
View
to a Worker Class . Never! . This can cause certain problems like NPE and Memory leaks ..Now coming to second problem You do not use
WorkManager
for async programming Normally .WorkManager
is more suited for scheduled task something you want to do in future and it might be deferred . That's more often called background task(basically when app is not opened) . Have look at This and This thread .If you want some async Operation to do you have basically two widely used options
Coroutines
andRxjava
. Since you are already usingkotlin
you can go withCoroutines
and also fact that its easy to implement.Now in such where you want to provide a callback to a UI component from a
Worker
you can follow This .Normally if your async task is an immediate one you do not use
Worker
you use aThread
in this case .Coroutines
is a multi threading framework so u can go for it .