在Android的doinbackground()中做UI任务



是否有办法在AsyncTask的doinbackground()中做UI任务。我很清楚最好在onPostExecute方法中这样做。但在我的情况下,因为我需要使用一个可重用的警报,能够访问UI在我的后台将节省我很多时间。也就是说,我需要在46个地方调整代码,但是能够在doinbackground中做到这一点只需要在一个地方进行更改。

Thanks in advance

希望这能解决你的问题

    onPreExecute() {
       // some code #1
    }
    doInBackground() {
        runOnUiThread(new Runnable() {
                    public void run() {
                        // some code #3 (Write your code here to run in UI thread)
                    }
                });
    }
    onPostExecute() {
       // some code #3
    }

除了从onPostExecute()更新UI之外,还有两种方法可以更新UI:

    runOnUiThread() 实现doInBackground()
  1. 源自onProgressUpdate()

通知你,

onProgressUpdate() -当你想从doInBackground()更新任何东西时,你只需要使用publishProgress(Progress...)来发布一个或多个进度单位,它将ping onProgressUpdate()来更新UI线程

您可以使用onProgressUpdate而不是

文档

最新更新