在 Android 中的 doinbackground() 中执行 UI 任务 [英] Doing UI task in doinbackground() in Android

查看:17
本文介绍了在 Android 中的 doinbackground() 中执行 UI 任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在 AsyncTask 的 doinbackground() 中做 UI 任务.我很清楚最好在 onPostExecute 方法中进行.但就我而言,由于我需要使用可重复使用的警报,因此能够在我的 doinbackground 中访问 UI 将为我节省大量时间.也就是说,我需要在 46 个地方调整代码,但是能够在 doinbackground 中做到这一点只需要在一个地方进行更改.

Is there a way to do UI task in the doinbackground() of the AsyncTask. I am well aware it is better to do it in onPostExecute method. But in my case since I am need to use a reusable alert, being able to access the UI in my doinbackground would save me a lot of time. That is, I need to tweak the code in 46 places but being able to do this in the doinbackground will need the change in just one place.

提前致谢

推荐答案

希望这能解决您的问题

    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
    }

这篇关于在 Android 中的 doinbackground() 中执行 UI 任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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