[英]Cancel AsyncTask with Progress Dialog button
我需要使用進度對話框中的按鈕來取消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)
@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();
myTask.cancel(true);
完全停止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
,因此發出警告。 它肯定會工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.