我需要在进度对话框中使用按钮取消我的AsyncTask。
我已使用按钮创建了“进度”对话框。 这是:
@Override
protected void onPreExecute() {
pDialog = new ProgressDialog(DSDactivity.this);
pDialog.setMessage(getResources().getString(R.string.pDialog));
pDialog.setCancelable(false);
pDialog.setButton("Cancel", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
pDialog.dismiss();
asyncCodelist.cancel(true);
}
});
pDialog.show();
}
我有两个问题:
点击"取消"在进度对话框中删除但异步 任务继续做他正在做的事
Eclipse让我渐渐渐渐渐渐失望:
AlertDialog类型的方法setButton(CharSequence,DialogInterface.OnClickListener)是 弃用
答案 0 :(得分:0)
@Override
protected void onPreExecute() {
pDialog = new ProgressDialog(DSDactivity.this);
pDialog.setMessage(getResources().getString(R.string.pDialog));
pDialog.setCancelable(true);
pDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
myTask.cancel(true);
dialog.dismiss();
}
}
}
然后,正如我的链接所说,创建你的AsyncTask并存储它:
MyAsyncTask myTask=null;
并执行它:
myTask = new MyAsyncTask();
myTask.execute();
答案 1 :(得分:0)
myTask.cancel(true);
正如文件所述:
To ensure that a task is cancelled as quickly as possible, you should always check the return value of isCancelled() periodically from doInBackground(Object[]), if possible (inside a loop for instance.)
setButton()
方法现在已弃用ProgressDialog
,因此警告。它肯定会奏效。