即使在加载视图后,我的ProgressDialog也不会被忽略。

时间:2012-06-25 08:03:31

标签: android android-dialog

我希望在加载视图之前显示Progress-Dialog。 首先我在onCreate()中编写了代码,但在这种情况下对话框没有出现。所以我在onResume()中写了它,但在这种情况下,即使在加载视图后它也不会消失。任何人都可以告诉我们这里出了什么问题?

              protected void onResume() {
    // TODO Auto-generated method stub

    super.onResume();
    dialog = ProgressDialog.show(this, "", "Please wait...", true); 
    //dialog.cancel();
    new Thread() 
    {
      public void run() 
      {

         try
           {

            sleep(1500);

      // do the background process or any work that takes time to see progress dialog

           }  
        catch (Exception e)
        {
            Log.e("tag",e.getMessage());
        }
    // dismiss the progressdialog   
     dialog.dismiss();
     }
    }.start();
    citySelected.setText(fetchCity);
    spinner.setSelection(getBG);
}

3 个答案:

答案 0 :(得分:1)

您无法从其他线程更新UI(在主UIthread中)。如果要在后台运行任何查询,可以使用AsyncTask。

在onPreExecute方法中,show dialog和onPostExecute可以关闭对话框。

如果要使用Thread,请使用处理程序更新UI。

使用AsyncTask

public class MyAsyncTask extends AsyncTask<String, Void, String> {
    ProgressDialog dialog = new ProgressDialog(ActivityName.this);
    @Override
    protected void onPreExecute() {
        dialog.show();
        super.onPreExecute();
    }

    @Override
    protected String doInBackground(String... params) {
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        dialog.dismiss();
        super.onPostExecute(result);
    }

}

在Activity onCreate方法中,

MyAsyncTask task = new MyAsyncTask();
    task.execute();

答案 1 :(得分:0)

您可以使用AsyncTask。它比Thread

更好
private class DownloadingProgressTask extends
        AsyncTask<String, Void, Boolean> {

    private ProgressDialog dialog = new ProgressDialog(ShowDescription.this);
    protected void onPreExecute() {
         this.dialog.setMessage("Please wait");
         this.dialog.show();

    }
    protected Boolean doInBackground(final String... args) {
        try {
                downloadFile(b.getString("URL"));
                return true;
        } catch (Exception e) {
                Log.e("tag", "error", e);
                return false;
        }
    }
    @Override
    protected void onPostExecute(final Boolean success) {

         if (dialog.isShowing()) {
         dialog.dismiss();
         }
    }
}

答案 2 :(得分:0)

最好使用Asynctask .........但如果您仍然想要know the solution only,那么可以尝试

new Thread() 
    {
      public void run() 
      {

         try
           {

            sleep(1500);

      // do the background process or any work that takes time to see progress dialog

           }  
        catch (Exception e)
        {
            Log.e("tag",e.getMessage());
        }




  YourActivity.this.runOnUIThread(new Runnable(){
                         @Override
                         public void run(){
                             // dismiss the progressdialog   
                              dialog.dismiss();
                        });





     }
    }.start();