问题描述
我正在使用 AsyncTask 将数据上传到 UI.我编写了代码以使用单独的方法从服务器下载数据,并且我正在从 doinBackground 调用该方法.它会出错,因为 UI 方法无法从 doInBackground.but 访问,但我想访问 .是否有任何替代过程可以从 doinBackground 访问 UI 方法.?
I am using AsyncTask to upload data to UI. i wrote the code to download data from server in a separate method and i am calling that method from doinBackground. It will give error because UI methods can't access from doInBackground.but, i want to access . any alternative process is there to access the UI method from doinBackground.?
推荐答案
在doInBackground()
中调用publishProgress()
.将 UI 更新逻辑放在 AsyncTask
的 onProgressUpdate()
中.在您调用 publishProgress()
后,将在主应用程序线程(也称为 UI 线程)上调用 onProgressUpdate()
.这是一个示例项目来演示这一点.
Call publishProgress()
in doInBackground()
. Put your UI-updating logic in onProgressUpdate()
of your AsyncTask
. onProgressUpdate()
will be called on the main application thread (a.k.a., UI thread) after you call publishProgress()
. Here is a sample project demonstrating this.
这篇关于Android:从 AsyncTask doInBackground 方法调用 UI 线程上的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!