AsyncTask 完成后无法关闭 ProgressDialog [英] Can't dismiss ProgressDialog after the AsyncTask complete

查看:20
本文介绍了AsyncTask 完成后无法关闭 ProgressDialog的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮忙,我无法在 AsyncTask 完成后关闭 ProgressDialog.我搜索了答案,但没有找到.当我使用 Thread 而不是 AsyncTask 时,此代码工作正常.有什么想法吗?

Please help, I can't dismiss ProgressDialog after AsyncTask complete. I searched for the answer but nothing found. This code works fine when I use Thread instead of AsyncTask. Have any ideas?

Context appContext;
ProgressDialog pd;

@Override
public void onCreate(Bundle savedInstanceState)
{
   super.onCreate(savedInstanceState);
   appContext=this;
   MyTask myTask=new MyTask(); 
   myTask.execute();
}


class MyTask extends AsyncTask<Void, Void, Void>
{
   @Override
   protected void onPreExecute() 
   {
      pd = ProgressDialog.show(appContext, "Wait..", "Loading data",
                    true, false);
  pd.setCancelable(false);
      super.onPreExecute();
   }

   @Override
   protected Void doInBackground(Void... arg0)
   {
  //something
  return null;
    }

   @Override
   protected void onPostExecute(Void result)
   {
      if (pd!=null)
        pd.dismiss();
  super.onPostExecute(result);
   }
}

推荐答案

   private ProgressDialog progressDialog;   // class variable       

   private void showProgressDialog(String title, String message)
   {
        progressDialog = new ProgressDialog(this);

        progressDialog.setTitle(title); //title

        progressDialog.setMessage(message); // message

        progressDialog.setCancelable(false);

        progressDialog.show();
   }         

onPreExecute()

    protected void onPreExecute()
    {
        showProgressDialog("Please wait...", "Your message");
    }

检查并关闭 onPostExecute() -

    protected void onPostExecute() 
    {
        if(progressDialog != null && progressDialog.isShowing())
        {
            progressDialog.dismiss();
        }
     }

这篇关于AsyncTask 完成后无法关闭 ProgressDialog的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆