警报对话框未显示

时间:2011-07-04 09:26:47

标签: android alertdialog

我在一个单独的线程中显示我的警告对话框,它不适用于我。最初我点击

3000ms的注册按钮我正在显示进度对话框。之后我想显示一个警告框,但它不起作用。怎么解决这个?

提前致谢...!

 register.setOnClickListener(new OnClickListener() {
  public void onClick(View v) {


Log.v(TAG, "Trying to Login");


   showDialog(0);
   t = new Thread() {
    public void run() {
     showDialog(0);
     try {
        Thread.sleep(3000);
        removeDialog(0);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    }
   };
   t.start();


  try {

 some data sending to server

 Object responce=(Object)soapEnvelope.getResponse();
   temp=responce.toString();
   if(temp.equals("1")){

 temp = "The result is 1";
         }

      System.out.println("The result is  "+temp);


        new Thread()
          {
             public void run()
             {
                     try
                          {

            sleep(3000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
           AlertDialog.Builder successfullyLogin = new Builder(Register.this);
            successfullyLogin.setCancelable(false);
            successfullyLogin.setMessage("Successfully Login !").show();
        //Toast.makeText(getBaseContext(), "Toast text", Toast.LENGTH_LONG).show();
               }
          }; 



          } catch (Exception e) {

          e.printStackTrace();
           }

             }

             });


               }

           @Override
            protected Dialog onCreateDialog(int id) {
             switch (id) {
              case 0: {
               dialog = new ProgressDialog(this);
              dialog.setMessage("Please wait while connecting...");
             dialog.setIndeterminate(true);
             dialog.setCancelable(true);

           }


             return dialog;
               }

            return null;
                 }

4 个答案:

答案 0 :(得分:3)

用AsyncTask类替换所有线程。它们专门针对此类事物而设计,与UI完美配合,用于显示对话框,并在UI线程中解除它们,同时仍在您需要的地方进行后台工作。

通过这种方式,您不需要3000ms超时,它只是在它返回时解除。当然,您可以计算登录所需的时间并保持对话直到您想要的3000毫秒,但我不会。此外,如果你想在Android中暂停使用SystemClock.sleep(3000);而不是java的本机线程休眠,因为你不需要尝试/捕获中断。

替换代码的示例(注意完全缺少线程,尝试/捕获等通常乱丢线程代码):

    // ... initialising onClickListener of your button to call the async task
    Button button = (Button) findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            new StartLoginAsyncTask(YOURAPP.this).execute((Void []) null);
        }
    });
}

private class StartLoginAsyncTask extends AsyncTask<Void, Void, Integer> {
    private ProgressDialog dialog;
    private final Context context;

    public StartLoginAsyncTask(Context context) {
        this.context = context;
    }

    @Override
    protected void onPreExecute() {
        // UI work allowed here
        dialog = new ProgressDialog(context);
        // setup your dialog here
        dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        dialog.setMessage(context.getString(R.string.please_wait_message));
        dialog.setCancelable(false);
        dialog.show();
    }

    @Override
    protected Integer doInBackground(Void... ignored) {
        Integer returnCode = doLogin();
        return returnCode;
    }

    @Override
    protected void onPostExecute(Integer returnCode) {
        // UI work allowed here
        dialog.dismiss();
        if (returnCode == LOGIN_OK) {
            // ... show other dialogs here that it was OK
        } else {
            // ... bad news dialog here
        }
    }
}

private Integer doLogin() {
    // ... write your login code here. 
    // This is run in background, do not do any UI work here
    return LOGIN_OK;
}

如果您希望登录中断对话框,则将自定义TimeoutException添加到doLogin(),在doInBackground()中捕获它,并返回相应的整数响应并处理它onPostExecute()

答案 1 :(得分:1)

我认为只能从主UI线程中显示对话框。因此,我们的想法是在主线程中有一个处理程序,并在新线程上发布消息,这将启动对话框。

答案 2 :(得分:1)

查看AsyncTask

从JavaDocs:AsyncTask可以正确,方便地使用UI线程。该类允许执行后台操作并在UI线程上发布结果,而无需操纵线程和/或处理程序。

答案 3 :(得分:1)

在内部线程中,你应该使用Handler来显示AlertDialog:

Handler messageHandler = new Handler() {
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
            case 111: 
                     // Build and show AlertDialog here
                     break;
                    }
                }
}

使用下面代替showdialog(0);

messageHandler.sendEmptyMessage(111);