【问题标题】:Showing Progress Dialog in AsyncTask在 AsyncTask 中显示进度对话框
【发布时间】:2018-04-15 19:40:05
【问题描述】:

您好,我必须在我的异步任务中显示一个进度对话框,我的异步任务是:

  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 正在查找,但当我单击按钮开始我的任务时,它没有显示进度对话框!

但每当我关闭配置文件对话框时,它就会开始向我显示进度对话框(忽略 onPostExecute)。

有人可以解决这个问题吗,我有点困惑为什么会这样!

【问题讨论】:

  • 你为什么在那里使用AsyncTask?无论如何,您都在 UI 线程上运行所有内容。另外,为什么不让 Glide 处理图像大小调整?
  • 问题是我在 RecyclerView Adapter 中显示这个,并且在 Glide 中设置之前我正在修改图像方向!
  • 我相信 Glide 也可以处理旋转。

标签: android android-asynctask progressdialog


【解决方案1】:

阅读ProgressDialog

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 方法中这样做?

【讨论】:

  • 我尝试将其设置为 first 和 last 但仍然没有显示进度对话框!
  • onPreExecute()AsyncTask 中为空。根本不需要调用super 方法。
  • @TheAngelCat 阅读此行You're running everything on the UI thread anyway. Also, why don't you let Glide handle the image resizing?
【解决方案2】:

试试这个,

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-31
    • 1970-01-01
    • 2013-01-04
    • 1970-01-01
    相关资源
    最近更新 更多