我有一個AsyncTask中的自定義進度對話框,它充當下載進度,我想在按下自定義取消按鈕時中斷doInBackground。似乎當對話框關閉時,doInBackground恢復無任何問題!DoInBackground對話框關閉時取消嗎?
1
A
回答
1
您可以從對話框的取消事件中調用AsyncTask.cancel(true)
。爲此,您需要對AsyncTask的引用,這可能是任務啓動時初始化的實例變量。然後在asyncTask.doInBackground()
方法中,您可以檢查isCancelled()
,或覆蓋onCancelled()
方法並停止正在運行的任務。
例子:
//Asynctask instance variable
private YourAsyncTask asyncTask;
//Starting the asynctask
public void startAsyncTask(){
asyncTask = new YourAsyncTask();
asyncTask.execute();
}
//Dialog code
loadingDialog = ProgressDialog.show(ThisActivity.this,
"",
"Loading. Please wait...",
false,
true,
new OnCancelListener()
{
@Override
public void onCancel(DialogInterface dialog)
{
if (asyncTask != null)
{
asyncTask.cancel(true);
}
}
});
編輯:如果您創建一個從裏面的AsyncTask的對話框,該代碼將不會有很大的不同。你可能不需要實例變量,我想你可以在這種情況下調用YourAsyncTask.this.cancel(true)。
1
I want to interrupt doInBackground when my custom cancel button pressed.
=>呼籲取消按鈕點擊事件裏面你的AsyncTask的cancel()
方法。現在,這不足以取消doInBackground()過程。
例如:
asyncTask.cancel(true);
要通知您已使用cancel()方法取消的AsyncTask,你必須檢查其是否取消或不使用isCancelled()
內doInBackground()
。
例如:
protected Object doInBackground(Object... x)
{
// do your work...
if (isCancelled())
break;
}
相關問題
- 1. 如何在doInBackground在asynctask中運行時關閉對話框?
- 2. 關閉對話框時從jquery對話框中獲取數據
- 3. Swing-取消對話框時關閉整個應用程序
- 4. javafx - 當對話框關閉時取消任務
- 5. 關閉輸入消息對話框
- 6. 對話框應該自動關閉嗎?
- 7. 如果對話框的對話框結果取消,則關閉當前表單
- 8. 從模態對話框返回時間時,關閉對話框
- 9. 我無法在關閉對話框中關閉對話框
- 10. 在Android中關閉對話框而不關閉對話框
- 11. 關閉對話框,關閉其主對話框
- 12. 如何以編程方式關閉/取消/關閉系統對話框(Android)
- 13. Android對話框關閉()不關閉
- 14. 關閉jQuery Datepicker關閉jQuery對話框
- 15. jQuery重置對話框取消和關閉表格發佈
- 16. 從Facebook取消權限對話框重定向但不關閉
- 17. Facebook共享對話框不關閉/取消
- 18. 如何關閉或取消自定義提醒對話框
- 19. 關閉一個對話框,點擊取消按鈕忽略EN_KILLFOCUS
- 20. jQuery對話框,關閉時隱藏對話框中的元素
- 21. 關閉jquery ui對話框時點擊了對話框
- 22. 線程完成時關閉對話框
- 23. 當點擊它時關閉對話框
- 24. 如何點擊時關閉對話框?
- 25. 關閉對話框時出現NullReferenceException
- 26. 關閉時重置模態對話框
- 27. 對話框自動關閉定時器
- 28. Android:創建時關閉對話框
- 29. 關閉對話框時的效果
- 30. jQuery對話框不關閉