Android:通过异步 Worker 类更改 TextView

发布于 2025-01-15 11:28:31 字数 1294 浏览 1 评论 0原文

我试图通过使用 WorkManagerWorker 类在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雨后彩虹 2025-01-22 11:28:31

您没有将 View 传递给 Worker Class 。绝不! 。这可能会导致某些问题,例如 NPE 和内存泄漏。

现在来讨论第二个问题,通常情况下,您不使用 WorkManager 进行异步编程。 WorkManager 更适合计划任务,您将来想做的事情,并且可能会被推迟。这通常被称为后台任务(基本上是在应用程序未打开时)。看看这个此线程

如果您想要执行一些异步操作,您基本上有两个广泛使用的选项 CoroutinesRxjava 。由于您已经在使用 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 and Rxjava . Since you are already using kotlin you can go with Coroutines 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 a Thread in this case . Coroutines is a multi threading framework so u can go for it .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文