从Android中的辅助线程调用主线程

时间:2010-12-08 14:13:18

标签: java android multithreading

如何从Android中的辅助线程调用主线程?

5 个答案:

答案 0 :(得分:81)

最简单的方法是从你的线程中调用runOnUiThread(...)

activity.runOnUiThread(new Runnable() {
    public void run() {
        ... do your GUI stuff
    }
});

答案 1 :(得分:23)

我建议在同一进程中传递线程是在这些线程之间发送消息。使用Handler来管理这种情况非常容易:

http://developer.android.com/reference/android/os/Handler.html

从Android文档到使用ui线程处理昂贵工作的使用示例:

public class MyActivity extends Activity {

    [ . . . ]
    // Need handler for callbacks to the UI thread
    final Handler mHandler = new Handler();

    // Create runnable for posting
    final Runnable mUpdateResults = new Runnable() {
        public void run() {
            updateResultsInUi();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        [ . . . ]
    }

    protected void startLongRunningOperation() {

        // Fire off a thread to do some work that we shouldn't do directly in the UI thread
        Thread t = new Thread() {
            public void run() {
                mResults = doSomethingExpensive();
                mHandler.post(mUpdateResults);
            }
        };
        t.start();
    }

    private void updateResultsInUi() {

        // Back in the UI thread -- update our UI elements based on the data in mResults
        [ . . . ]
    }
}

答案 2 :(得分:1)

您需要一个Handler将信息传递回主线程。

答案 3 :(得分:1)

另外,最好记住,如果你通过AsyncTask获得辅助线程,你可以选择调用onProgressUpdate()onPostExecute()等来完成主线程的工作。< / p>

答案 4 :(得分:1)

使用HandlerThread

的示例代码
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final Handler responseHandler = new Handler(Looper.getMainLooper()){
            @Override
            public void handleMessage(Message msg) {
                //txtView.setText((String) msg.obj);
                Toast.makeText(MainActivity.this,
                        "Result from UIHandlerThread:"+(int)msg.obj,
                        Toast.LENGTH_LONG)
                        .show();
            }
        };

        HandlerThread handlerThread = new HandlerThread("UIHandlerThread"){
            public void run(){
                /* Add your business logic to pupulate attributes in Message
                   in place of sending Integer 5 as in example code */
                Integer a = 5;
                Message msg = new Message();
                msg.obj = a;
                responseHandler.sendMessage(msg);
                System.out.println(a);
            }
        };
        handlerThread.start();

    }

}

<强>解释

  1. 在上面的示例中,HandlerThread在{1}}的UI主题上发布Message,该主题已使用{1}}的UI主题初始化。

    Handler
  2. Looperfinal Handler responseHandler = new Handler(Looper.getMainLooper()) responseHandler.sendMessage(msg);发送到UI主题Message

  3. HandlerThreadHandler上收到的handleMessage进程,并在UI线程上显示Toast。