こんにちは私のアプリには連絡先の詳細を読み取るための非同期タスクがあります(これには少し時間がかかります)。それはうまくいきますが、完了する前に非同期タスクをキャンセルすると、アプリがクラッシュする詳細を取得するときに問題が発生します。非同期タスクをキャンセルしたときにアプリを終了させると考えていました。Webを検索していくつかの方法を見つけましたが、機能しませんでした。非同期タスクをキャンセルしたときにアプリを終了するにはどうすればよいですか? 私のAsynタスクコード(通常のコード)
public class FetchingContact extends AsyncTask<String, Void, Void> {
private final ProgressDialog dialog = new ProgressDialog(
MobiMailActivity.this);
// can use UI thread here
protected void onPreExecute() {
this.dialog.setMessage("Fetching Contact...");
this.dialog.show();
}
// automatically done on worker thread (separate from UI thread)
protected Void doInBackground(final String... args) {
readContact();
if (isCancelled ()) {
finish();
}
return null;
}
// can use UI thread here
protected void onPostExecute(final Void unused) {
if (this.dialog.isShowing()) {
this.dialog.dismiss();
CharSequence test=sam;
// search_sort.setText(test);
check(test);
}
// reset the output view by retrieving the new data
// (note, this is a naive example, in the real world it might make
// sense
// to have a cache of the data and just append to what is already
// there, or such
// in order to cut down on expensive database operations)
// new SelectDataTask().execute();
}
}