如何从一个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.