Android:从 AsyncTask doInBackground 方法调用 UI 线程上的方法 [英] Android : Calling the methods on UI thread from AsyncTask doInBackground method

查看:26
本文介绍了Android:从 AsyncTask doInBackground 方法调用 UI 线程上的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 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 访问 UI 方法.?

any alternative process is there to access the UI method from doinBackground.?

doInBackground() 中调用publishProgress().将 UI 更新逻辑放在 AsyncTaskonProgressUpdate() 中.在您调用 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 线程上的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆