Hello我必须在异步任务中显示进度对话框,我的异步任务是:
private class UserProfileDialog extends AsyncTask<String, String, String> {
private String imagePath;
private String name;
private String status;
private String number;
private Activity activity;
private ProgressDialog progressDialog;
public UserProfileDialog(Activity activity) {
this.activity = activity;
}
@Override
protected void onPreExecute() {
progressDialog = new ProgressDialog(activity);
progressDialog.setMessage("Please Wait...");
progressDialog.setCancelable(false);
progressDialog.show();
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
imagePath = params[0];
name = params[1];
status = params[2];
number = params[3];
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
try {
//show the dialog for dob date picker
final Dialog profileDialog = new Dialog(context);
profileDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
profileDialog.setContentView(R.layout.profile_dialog_me);
TextView title = (TextView) profileDialog.findViewById(R.id.title);
title.setTypeface(EasyFonts.robotoBold(context));
ImageView imageCloser = (ImageView) profileDialog.findViewById(R.id.imageCloser);
final ImageView profilePicture_x = (ImageView) profileDialog.findViewById(R.id.profilePicture);
//set the profile picture
Bitmap originalBitmap = BitmapFactory.decodeFile(new File(imagePath).getAbsolutePath());
originalBitmap = ModifyOrientationUtility.modifyOrientation(originalBitmap, new File(imagePath).getAbsolutePath());
Bitmap bitmap = resizeBitmapFitXY(400, 400, originalBitmap);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
Glide.with(context)
.load(stream.toByteArray())
.asBitmap()
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true)
.dontAnimate()
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap arg0, GlideAnimation<? super Bitmap> arg1) {
// TODO Auto-generated method stub
profilePicture_x.setImageBitmap(arg0);
}
});
TextView profileName = (TextView) profileDialog.findViewById(R.id.profileName);
profileName.setTypeface(EasyFonts.robotoLight(context));
profileName.setText(name);
TextView profileNumber = (TextView) profileDialog.findViewById(R.id.profileNumber);
profileNumber.setTypeface(EasyFonts.robotoLight(context));
profileNumber.setText(number);
TextView profileStatus = (TextView) profileDialog.findViewById(R.id.profileStatus);
profileStatus.setTypeface(EasyFonts.robotoLight(context));
profileStatus.setText(status);
//UI Events
imageCloser.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
profileDialog.dismiss();
}
});
//show the dialog
profileDialog.show();
} catch (Exception exp) {
MyMLogger.showMLog("xm", "show profile dialog " + exp.toString(), logFromClass, "showProfileDialog");
exp.printStackTrace();
}
}
});
return null;
}
@Override
protected void onPostExecute(String s) {
if (progressDialog.isShowing()) {
progressDialog.dismiss();
}
super.onPostExecute(s);
}
}
AsyncTask正在运行find但是当我按下按钮时启动任务时它没有显示进度对话框!
但每当我关闭配置文件对话框时,它就会开始显示进度对话框(忽略onPostExecute)。
有人可以把问题搞清楚,我有点困惑为什么会发生这种情况!
答案 0 :(得分:1)
super.onPreExecute();
应该删除。
@Override
protected void onPreExecute() {
// super.onPreExecute();
progressDialog = new ProgressDialog(activity);
progressDialog.setMessage("Please Wait...");
progressDialog.setCancelable(false);
progressDialog.show();
}
<强>问题强>
//UI Events
imageCloser.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
profileDialog.dismiss();
}
});
显示进度指示器和可选文本消息的对话框 视图。只能同时使用短信或视图。
为什么要在 doInBackground
方法中执行此操作?
答案 1 :(得分:0)
试试这个,
private ProgressDialog pdia;
@Override
protected void onPreExecute(){
super.onPreExecute();
pdia = new ProgressDialog(yourContext);
pdia.setMessage("Loading...");
pdia.show();
}
@Override
protected void onPostExecute(String result){
super.onPostExecute(result);
if(pdia.isShownig){
pdia.dismiss();
}
}
同时查看此链接, progressDialog in AsyncTask