如何从一个Android AsyncTask的活动用户界面的变化?

编程入门 行业动态 更新时间:2024-10-27 10:30:17
本文介绍了如何从一个Android AsyncTask的活动用户界面的变化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在这样一个场景,我有一个用户界面,将从(使用AsyncTask的)一个单独的线程更新,我可以定义AsyncTask的作为一个内部类的活动,但这样做有两个缺点,我觉得有问题的:

In a scenario where I have a UI that will be updated from a separate thread (using AsyncTask), I can define the AsyncTask as an inner class of the activity, but this has two downsides I find problematic:

  • 这使得源文件非常大,效率降低在管理code
  • 这使得它很难重用线程类
  • 有什么好的解决办法?使用一个内部类,但抽象它所做的一切其他类?引用传递给活动的AsyncTask的?始终定义的AsyncTask类作为一个内部类,并只接受源文件会很大?

    What's a good solution? Use an inner class, but abstract everything it does to other classes? Pass a reference to the Activity to the AsyncTask? Always define the AsyncTask class as an inner class and just accept source files will be large?

    推荐答案

    我看到只是通过一个上下文到的AsyncTask 。

    Quite a few examples I have seen just pass a Context into the constructor of the AsyncTask.

    public class BackgroundStuff extends AsyncTask<Void, Void, Void> { ... Context mContext; ... BackgroundStuff(Context context){ super(); this.mContext = context; } ... }

    我很想听到的话,任何人使用任何其他办法。

    I would be interested to hear if anyone else uses any other approaches.

    更多推荐

    如何从一个Android AsyncTask的活动用户界面的变化?

    本文发布于:2023-06-09 06:29:02,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/596855.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:用户界面   Android   AsyncTask

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!