我希望在我的应用程序中使用AsyncTask来管理一些业务逻辑。对于在单独的文件中定义的AsyncTask的onProgressUpdate(...)方法,最佳模式是什么?
我有两个想法:
1. 最简单的方法:在Activity中创建ProgressDialog(使用onCreateDialog(...)方法),并通过构造函数将引用传递给我的AsyncTask子类(在我的AsyncTask子类中覆盖onProgressUpdate(...))。这种解决方案的缺点是在业务逻辑代码中使用UI组件。
我有两个想法:
1. 最简单的方法:在Activity中创建ProgressDialog(使用onCreateDialog(...)方法),并通过构造函数将引用传递给我的AsyncTask子类(在我的AsyncTask子类中覆盖onProgressUpdate(...))。这种解决方案的缺点是在业务逻辑代码中使用UI组件。
FooTask1.java:
public class FooTask1 extends AsyncTask<Void, Integer, Void> {
private ProgressDialog mProgressDialog;
public FooTask1(ProgressDialog progressDialog) {
super();
mProgressDialog = progressDialog;
}
@Override
protected Void doInBackground(Void... unused) {
// time consuming operation
for (int i=0; i<=100; i++) {
this.publishProgress(i);
try {
Thread.sleep(100);
} catch (Exception e) {}
}
return null;
}
@Override
protected void onProgressUpdate(Integer... progress) {
mProgressDialog.setProgress(progress[0]);
}
@Override
protected void onPostExecute(Void result) {
mProgressDialog.dismiss();
}
}
FooActivity1.java:
public class FooActivity1 extends Activity {
private static final int DIALOG_PROGRESS_ID = 0;
private ProgressDialog mProgressDialog;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
showDialog(DIALOG_PROGRESS_ID);
new FooTask(mProgressDialog).execute();
}
@Override
protected Dialog onCreateDialog(int id) {
switch(id) {
case DIALOG_PROGRESS_ID:
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.setMessage("Loading...");
mProgressDialog.setCancelable(false);
return mProgressDialog;
default:
return null;
}
}
}
2. 更为复杂的方法:在Activity
类中重写AsyncTask
的onProgressUpdate(...)
方法:
FooTask2.java:
public class FooTask2 extends AsyncTask<Void, Integer, Void> {
@Override
protected Void doInBackground(Void... unused) {
// time consuming operation
for (int i=0; i<=100; i++) {
this.publishProgress(i);
try {
Thread.sleep(100);
} catch (Exception e) {}
}
return null;
}
}
FooActivity2.java
public class FooActivity2 extends Activity {
private static final int DIALOG_PROGRESS_ID = 0;
private ProgressDialog mProgressDialog;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
showDialog(DIALOG_PROGRESS_ID);
new FooTaskLoader().execute();
}
@Override
protected Dialog onCreateDialog(int id) {
switch(id) {
case DIALOG_PROGRESS_ID:
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.setMessage("Loading...");
mProgressDialog.setCancelable(false);
return mProgressDialog;
default:
return null;
}
}
private class FooTaskLoader extends FooTask2 {
@Override
protected void onProgressUpdate(Integer... progress) {
mProgressDialog.setProgress(progress[0]);
}
@Override
protected void onPostExecute(Void result) {
dismissDialog(DIALOG_PROGRESS_ID);
}
}
}