本文介绍了runOnUiThread 方法和 Handler 有什么区别?哪一种最好用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通常使用的方法

 runOnUiThread (new Runnable () {

     @Override
     public void run () {

     }

 });

在主线程中启动一些进程.就在最近我发现了这个

to launch some prcess in the main thread. just recently I discovered this one

new Handler(Looper.getMainLooper()).post(new Runnable () {

    @Override
    public void run () {
        // this will run in the main thread
    }

});

我的问题是这两种方法有什么区别,哪一种最好用?

My question is what is the difference between the two methods and which one is the best to use?

推荐答案

两者其实是一样的.runOnUiThreadHandler#post 都在 UI 线程中运行传递的 Runnable.

Both are actually same. Both runOnUiThread and Handler#post runs the passed Runnable in the UI Thread.

仅供参考,您还可以通过调用方法 View#post(runnable)View 的帮助下在 UI 线程上执行任何 Runnable>.

FYI, you can also execute any Runnable on UI Thread with the help of any View by calling the method View#post(runnable).

由于所有方法内部都使用了Handler,所以都是一样的,使用这些方法没有任何区别.

Since all approaches uses Handler internally, all are same and there won't be any difference in using any of these.

这篇关于runOnUiThread 方法和 Handler 有什么区别?哪一种最好用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 15:49