不显示警告对话框 [英] alert dialog not showing

查看:144
本文介绍了不显示警告对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我显示我的提醒对话框中一个单独的线程和它不是为​​我工作。最初,当我点击

注册按钮3000ms我显示进度对话。之后,我想展示一个警告框,但它不工作。如何解决这个问题?

在此先感谢...!

  register.setOnClickListener(新OnClickListener(){
  公共无效的onClick(视图v){


Log.v(TAG,试图登录);


   的ShowDialog(0);
   T =新的Thread(){
    公共无效的run(){
     的ShowDialog(0);
     尝试 {
        视频下载(3000);
        removeDialog(0);
    }赶上(InterruptedException异常E){
        // TODO自动生成的catch块
        e.printStackTrace();
    }

    }
   };
   t.start();


  尝试 {

 一些数据发送到服务器

 对象的反响=(对象)soapEnvelope.getResponse();
   临时= responce.toString();
   如果(temp.equals(1)){

 TEMP =结果是1;
         }

      的System.out.println(结果是+临时);


        新的Thread()
          {
             公共无效的run()
             {
                     尝试
                          {

            睡眠(3000);
        }赶上(InterruptedException异常E){
            // TODO自动生成的catch块
            e.printStackTrace();
        }
           AlertDialog.Builder successfullyLogin =新生成器(Register.this);
            successfullyLogin.setCancelable(假);
            successfullyLogin.setMessage(登录成功!)显示()。
        //Toast.makeText(getBaseContext(),多士文,Toast.LENGTH_LONG).show();
               }
          };



          }赶上(例外五){

          e.printStackTrace();
           }

             }

             });


               }

           @覆盖
            受保护的对话框onCreateDialog(INT ID){
             开关(ID){
              情况下0:{
               对话框=新ProgressDialog(本);
              dialog.setMessage(请等待连接......);
             dialog.setIndeterminate(真正的);
             dialog.setCancelable(真正的);

           }


             返回对话框;
               }

            返回null;
                 }
 

解决方案

替换所有的线程与AsyncTask的类。它们专为这种类型的事情设计,并与用户界面完美的工作显示对话框,并驳回他们在UI线程,同时还在做后台工作,在你需要它。

在这种方式,你不需要3000ms超时,它只是驳回当它返回。当然,你可以多长时间登录需要并保持对话,直到你3000ms是,如果你想,但我不会。另外,如果要暂停在Android中使用 SystemClock.sleep(3000); ,而不是Java的本地线程睡眠,因为你并不需要的try / catch中断。

替换您的code的例子(注意,完全没有线程,尝试/捕获等,通常垃圾线程code):

  // ...初始化你的按钮onClickListener调用异步任务
    Button按钮=(按钮)findViewById(R.id.button);
    button.setOnClickListener(新View.OnClickListener(){
        公共无效的onClick(视图v){
            新StartLoginAsyncTask(YOURAPP.this).execute((无效[])NULL);
        }
    });
}

私有类StartLoginAsyncTask扩展的AsyncTask<虚空,虚空,整数GT; {
    私人ProgressDialog对话框;
    私人最终上下文的背景下;

    公共StartLoginAsyncTask(上下文的背景下){
        this.context =背景;
    }

    @覆盖
    在preExecute保护无效(){
        //这里的UI工作允许
        对话框=新ProgressDialog(上下文);
        //设置您在这里对话
        dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        dialog.setMessage(context.getString(R.string.please_wait_message));
        dialog.setCancelable(假);
        dialog.show();
    }

    @覆盖
    保护整数doInBackground(空...忽略){
        整数返回code = doLogin();
        返回返回code;
    }

    @覆盖
    保护无效onPostExecute(整数返回code){
        //这里的UI工作允许
        dialog.dismiss();
        如果(返程code == LOGIN_OK){
            // ...显示其它对话框在这里,就OK了
        } 其他 {
            // ...坏消息在这里对话
        }
    }
}

私人整数doLogin(){
    // ...在这里写您的登录code。
    //这是在后台运行,没有做任何的用户界面在这里工作
    返回LOGIN_OK;
}
 

如果你想登录中断的对话框,然后自定义 TimeoutException异常添加到 doLogin(),赶在 doInBackground(),并返回相应的整数响应,并将其在 onPostExecute处理()

I am showing my alert dialog in a separate thread and its not working for me. initially when I click

register button for 3000ms I am showing a progress dialogue. and after that I want to show a alert box but its not working. How to solve this?

Thanks in advance...!

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

解决方案

Replace all your threading with AsyncTask classes. They are designed specifically for this type of thing, and work perfectly with the UI for showing dialogs, and dismissing them in the UI thread while still doing background work where you need it.

In this way, you don't need the 3000ms timeout, it just dismisses when it returns. Of course, you could time how long the login takes and keep the dialog up until your 3000ms is up if you want to, but I wouldn't. Also, if you want to pause in Android use SystemClock.sleep(3000); instead of java's native thread sleep, as you don't need to try/catch the interrupt.

An example that replaces your code (notice the complete lack of threads, try/catches etc that usually litter threading code):

    // ... 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;
}

If you want the login to interrupt the dialog, then add a custom TimeoutException to the doLogin(), catch it in the doInBackground(), and return the appropriate Integer response and handle it in onPostExecute().

这篇关于不显示警告对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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