我已经实现了一个简单的自定义进度对话框,我在onPreExecute方法中显示了该对话框。但是在显示对话框之后AsycnTask没有进展到doInBackground方法。因此,对话框永远显示出来。当我注释掉对话框显示它工作正常。请参阅下面的代码。我该如何解决这个问题。
public class CustomProgressDialog extends ProgressDialog
{
public CustomProgressDialog(Context context) {
super(context);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.customprogress);
}
@Override
public void show() {
super.show();
}
}
private CustomProgressDialog getProgressDialog() {
CustomProgressDialog p = new CustomProgressDialog(activity);
p.setCancelable(true);
p.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
loadMainFragment();
if(imageTask != null)
imageTask.cancel(true);
}
});
private class ImageDownloader extends AsyncTask<String, Void, Bitmap> {
CustomProgressDialog ringProgressDialog = null;
Bitmap bitmap = null;
@Override
protected Bitmap doInBackground(String... param) {
Bitmap b = downloadBitmap(param[0]);
image = b;
return b;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
ringProgressDialog = getProgressDialog()
ringProgressDialog.show();
// after showing this no progression
}
@Override
protected void onPostExecute(Bitmap result) {
if(isCancelled())
return;
if (result == null)
return;
super.onPostExecute(result);
bitmap = result;
// do work
if(ringProgressDialog != null)
ringProgressDialog.dismiss();
}
private Bitmap downloadBitmap(String url) {
Bitmap bm = null;
InputStream is = null;
try {
URL aURL = new URL(url);
URLConnection conn = aURL.openConnection();
conn.setConnectTimeout(Utils.timeout);
conn.setReadTimeout(Utils.timeout);
conn.connect();
is = conn.getInputStream();
bm = Utils.decodeBitmapFromInputStream(is,width, width, true);}
catch(Exception e){}
return bm;
}