且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

AsyncTask的不能在Android的线程工作

更新时间:2023-09-17 16:37:10

我建议你咨询 AsyncTask的文档进程和线程以更好地理解它是如何工作。从本质上讲,你应该在主线程创建 AsyncTask的子类。

I recommend you consult the AsyncTask documentation and Processes and Threads for a better understanding of how it works. Essentially, you should create your AsyncTask subclass on the main thread.

当你调用AsyncTask.execute(),您提供的,AsyncTask.on$p$pExecute被称为主线程,所以你可以做的UI设置。

When you call AsyncTask.execute(), your provided, AsyncTask.onPreExecute is called on the main thread, so you can do UI setup.

下一页AsyncTask.doInBackground方法被调用,并在其自己的线程中运行。

Next AsyncTask.doInBackground method is called, and runs in its own thread.

最后,当你的AsyncTask.doInBackground方法完成后,一个调用,以AsyncTask.onPostExecute在主线程上,你可以做任何UI清理工作。

Finally, when your AsyncTask.doInBackground method completes, a call is made to AsyncTask.onPostExecute on the main thread, and you can do any UI cleanup.

如果您需要更新来自内部的AsyncTask.doInBackground方法,调用AsyncTask.publishProgress,这将调用onProgressUpdate在主线程。

If you need to update the UI from within your AsyncTask.doInBackground method, call AsyncTask.publishProgress, which will invoke onProgressUpdate in the main thread.