简体   繁体   English

带有事件监听器的Android多线程

[英]Android multithreading with event listeners

I'm in my Android app I have the UI Thread (obviously) and also some other Threads I use to do some work. 我在我的Android应用程序中我有UI线程(显然)以及我用来做一些工作的其他一些线程。 There are listeners in my UI thread waiting for the work to be completed in the other threads (let's call it workComplete event). 在我的UI线程中有监听器等待在其他线程中完成工作(让我们称之为workComplete事件)。

I'm facing an issue right there. 我正面临一个问题。 When my listener receives the call, the current thread is the worker thread, not the UI thread. 当我的侦听器收到调用时,当前线程是工作线程,而不是UI线程。 So, if I try to do something that should come from the UI thread (modifying a view, etc.), it breaks or gives a warning. 因此,如果我尝试做一些应该来自UI线程(修改视图等)的东西,它会中断或发出警告。

My question is: what's the best approach for this? 我的问题是:对此最好的方法是什么? I wanted to be back in the UI thread when worker finished it's job and calls listener's workComplete event. 当工作人员完成它的工作并调用监听器的workComplete事件时,我想回到UI线程中。

Thanks! 谢谢!

The approach to get back on the UI thread commonly used is to post using a Handler that was originally created on the UI thread: 回到常用的UI线程的方法是使用最初在UI线程上创建的Handler进行发布:

//create thread on UI Thread (associates with Looper)
Handler handler = new Handler();

//then use it in a background thread
handler.post(new Runnable(){
    public void run(){
        //back on UI thread...
    }
}

You can use the runOnUiThread() method in the Activity class. 您可以在Activity类中使用runOnUiThread()方法。

From the documentation: 从文档:

void android.app.Activity.runOnUiThread(Runnable action) void android.app.Activity.runOnUiThread(Runnable action)

Runs the specified action on the UI thread. 在UI线程上运行指定的操作。 If the current thread is the UI thread, then the action is executed immediately. 如果当前线程是UI线程,则立即执行该操作。 If the current thread is not the UI thread, the action is posted to the event queue of the UI thread. 如果当前线程不是UI线程,则将操作发布到UI线程的事件队列。

Parameters: action the action to run on the UI thread 参数:action要在UI线程上运行的操作

So in your workComplete event: 所以在你的workComplete事件中:

runOnUiThread(new Runnable() {
    public void run() {
         // Do some cool UI stuff here ...
    }
}

If you're inside a fragment you can call getActivity().runOnUiThread(runnable) . 如果你在一个片段中,你可以调用getActivity().runOnUiThread(runnable)

According to me you have to use Thread pool Executor and even google have sample code which will describe everything how image is submitted to multiple thread how to submit task when download and decode completed and update ui thread it's best example for me to learn and it help me a lot. 根据我,你必须使用线程池Executor,甚至谷歌有示例代码,将描述图像提交到多个线程的一切如何提交任务下载和解码完成和更新ui线程它是我学习的最好的例子,它的帮助我很多

https://developer.android.com/training/multiple-threads/create-threadpool.html https://developer.android.com/training/multiple-threads/create-threadpool.html

handler 处理器

https://developer.android.com/training/multiple-threads/communicate-ui.html https://developer.android.com/training/multiple-threads/communicate-ui.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM