【问题标题】:Show process dialog box on downloading下载时显示进程对话框
【发布时间】:2014-04-15 11:14:18
【问题描述】:

我正在使用进程对话框功能来显示对话框。问题是对话框首先显示进程,然后进入运行方法,但我希望它在显示进程时移动到运行方法中。我怎样才能做到这一点...

AlertDialog.Builder ob=new AlertDialog.Builder(this);
            ob.setTitle("Confrimation").setMessage("Are you sure you want to logout?");

            ob.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
                public void onClick(DialogInterface di1,int id)
                {
                    dialog.cancel();
                }
            });
            ob.setPositiveButton("OK", new DialogInterface.OnClickListener(){
                public void onClick(DialogInterface di2,int id)
                {
                    dialog = ProgressDialog.show(Questionnaire.this, "Processing","Downloading survey...");
                     Thread t=new Thread()
                     {
                         public void run()
                         {
                            // delete database values
                              deleteDatabaseValues();
                              Log.e("inside","dialogbox");
                              downloadDatabase();
                              dialog.dismiss();
                        }
                    }; 
                    Handler handler=new Handler();
                    handler.postDelayed(t,60000);

                }
            });

            ob.show();
}

【问题讨论】:

  • 使用了 AsynsTask 而不是 Thread 和

标签: android android-alertdialog dialog


【解决方案1】:

您必须使用 Asynctask 来完成您提到的行为。

在此处查看示例代码--

    class ProcessData extends AsyncTask<String, String, String> {

    @Override
    protected void onPreExecute() 
    {
       super.onPreExecute();
       dialog = ProgressDialog.show(Questionnaire.this, 
       "Processing","Downloading survey...");
    }


    protected String doInBackground(String... args) 
    {

         // delete database values
          deleteDatabaseValues();
          Log.e("inside","dialogbox");
          downloadDatabase();
          dialog.dismiss();
          return ""; //or something you want to return
    }


    protected void onPostExecute(String result) 
    {
       //Do anything with the result

       dialog.dismiss();
    }

记得调用new ProcessData().execute(&lt;your params&gt;)来调用上面的Asynctask。

【讨论】:

    【解决方案2】:

    你需要实现一个 asyncTask:

    • onPreExecute: startProgressDialog
    • doInBackground:下载数据库
    • onPostExecute: stopProgressDialog

    【讨论】:

      【解决方案3】:

      使用异步任务。试试这个:

      public class BackTask extends AsyncTask<Integer, Integer, Integer> {
      
      private final ProgressDialog dialog;
      
      @Override
      protected void onPreExecute() {
          super.onPreExecute();
      
          dialog = new ProgressDialog(YourActivity.this);
          dialog.setTitle("Title");
          dialog.setMessage("Message");
          dialog.setCancelable(false);
      
          dialog.show();
      }
      
      @Override
      protected Integer doInBackground(Integer... arg0) {
      
          download();
      
          return 0;
      }
      
      @Override
      protected void onPostExecute(Integer result) {
      
          if (dialog.isShowing()) {
              dialog.dismiss();
          }
      
          //Post execute tasks
      }}
      

      然后调用它:

      new BackTask().execute();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-22
        • 1970-01-01
        • 2012-10-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多