下载时显示进程对话框

时间:2014-04-15 11:14:18

标签: android alertdialog dialog

我正在使用进程对话框功能来显示对话框。问题是对话框首先显示进程然后进入内部运行方法但我希望它在显示进程时在run方法内移动。我怎么能实现这个......

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();
}

2 个答案:

答案 0 :(得分:0)

您需要实现asyncTask:

  • onPreExecute:startProgressDialog
  • doInBackground:downloadDatabase
  • onPostExecute:stopProgressDialog

答案 1 :(得分:0)

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

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();