use*_*985 5 android android-asynctask
是否有可能定义AsyncTask的单个实例,而不是在每个类中定义?
或者它被设计为每个活动单独定义?
我有4-5个活动,每个活动都有不同的url请求,对于url的响应,我已经定义了一个单独的类,其中包含根据请求解析和填充对象的方法.
编辑:我上面犯了一个错误.实际上,我应该定义从AsyncTask扩展的单个类,还是在需要的每个Activity中定义一个私有类?
道歉,我有点困惑.
谢谢.
希望这可以帮助你:
public class MyAsynctask extends AsyncTask<String, Integer, Integer> {
@Override
protected Integer doInBackground(String... params) {
int i = 0;
for (int x = 0; x < 500; x++) {
i++;
}
return i;
}
@Override
protected void onPostExecute(Integer result) {
super.onPostExecute(result);
}
}
Run Code Online (Sandbox Code Playgroud)
活动使用Asynctask:
public class MyAsyncUser extends Activity{
int result;
TextView view;
public void onCreate(android.os.Bundle savedInstanceState) {
view = new TextView(this);
new MyAsynctask(){
protected void onPostExecute(Integer result) {
view.setText("" + result);
}
}.execute("what to do");
}
}
Run Code Online (Sandbox Code Playgroud)
因此,您可以定义一个Asynctask类并使用它,并在不同的地方创建一些结果.
| 归档时间: |
|
| 查看次数: |
4157 次 |
| 最近记录: |