我试图在 AsyncTask 的“doInBackGround”方法运行时从主线程取消一个对话框。当我下载照片时,会弹出一个进度对话框,下载完成后,我在 onPostExecute 中关闭()对话框。如果连接速度很慢,对话框会持续一段时间,我无法取消它,直到出现超时错误或完成下载。如何使用后退按钮以便主线程可以访问。这是我的代码的样子:
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);
}
}