如何从一个Android AsyncTask的活动用户界面的变化? [英] How to make activity UI changes from an Android AsyncTask?

查看:123
本文介绍了如何从一个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:

  1. 这使得源文件非常大,效率降低在管理code
  2. 这使得它很难重用线程类

有什么好的解决办法?使用一个内部类,但抽象它所做的一切其他类?引用传递给活动的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?

推荐答案

我看到只是通过一个上下文到<$ C $的构造有不少例子C>的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的活动用户界面的变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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