AsyncTask の「doInBackGround」メソッドの実行中にメインスレッドからダイアログをキャンセルしようとしています。写真のダウンロード中に進行状況ダイアログがポップアップ表示され、ダウンロードが完了すると onPostExecute でダイアログを dismiss() します。接続が遅い場合、ダイアログがしばらく表示され、タイムアウト エラーが発生するか、ダウンロードが完了するまでキャンセルできません。メインスレッドがアクセスできるように戻るボタンを使用するにはどうすればよいですか。私のコードは次のようになります。
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
protected void onPreExecute() {
//this piece code doesn't seem to work
progressDialog = ProgressDialog.show(context, "",
"Image loading", true);
}
protected Bitmap doInBackground(String... urls) {
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
//bmImage.setImageBitmap(result);
progressDialog.dismiss();
someMethod(result);
}
}