Update the UI outside the main thread

后端 未结 5 1158
梦谈多话
梦谈多话 2021-01-17 01:21

I am totaly new to android and just want to know if it is any working and possible way to update the UI outside the main thread. Just from my code I have listed below I know

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-17 01:48

    You can't update UI directly from non-UI thread, but You could communicate with UI thread using Handler object or AsyncTask object. The most convinient way to use AsyncTask:

    1. .doInBackground() method of AsyncTask runs in non-ui thread
    2. .onProgressUpdate() runs in UI thread so could change views
    3. You could use publishProgress() method inside doInBackground() to pass data to .onProgressUpdate.

    Sorry if some mistakes in method names. Read http://developer.android.com/guide/components/processes-and-threads.html for details.

提交回复
热议问题