+-
java – 我们什么时候需要在android应用程序中使用runOnUiThread?
我有一个代码示例,它使用此函数来运行线程runOnUiThread.我们为什么以及何时需要使用它?

编辑

如何使用AsyncTask类,有什么优点和缺点?

最佳答案
当您想要从非UI线程更新UI时,必须使用runOnUiThread().例如 – 如果要从后台线程更新UI.你也可以使用Handler做同样的事情.

来自文件 –

Runs the specified action on the UI thread. If the current thread is
the UI thread, then the action is executed immediately. If the current
thread is not the UI thread, the action is posted to the event queue
of the UI thread.

句法 –

       Activity_Name.this.runOnUiThread(new Runnable() {

            @Override
            public void run() {
                // your stuff to update the UI

            }
        });

更新 –

AsyncTask -

If you want to do some Network operation or anything that blocks
your UI in that case AsyncTask is best options. There are several
other ways for performing the same Background Operations as you can
use Service, IntentService also for doing Background Operations.
Using AsyncTask will help you doing your UI work and also won’t block
your UI until your background Operation is going on.

来自文件 –

AsyncTask enables proper and easy use of the UI thread. This class
allows to perform background operations and publish results on the UI
thread without having to manipulate threads and/or handlers.

点击查看更多相关文章

转载注明原文:java – 我们什么时候需要在android应用程序中使用runOnUiThread? - 乐贴网