1

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;
                 }
Randroid
  • 3,688
  • 5
  • 30
  • 55

4 Answers4

3

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

Mark Fisher
  • 9,838
  • 3
  • 32
  • 38
  • Fisher, Thanks a lot and can we use string in place of integer here in this line private class StartLoginAsyncTask extends AsyncTask coz i m getting string value as response from my server – Randroid Jul 04 '11 at 10:10
  • Yes, you define the return type in the `AsyncTask` line, then change the return/parameter types as needed in the `doInBackground()` and `onPostExecute()`. – Mark Fisher Jul 04 '11 at 10:39
1

I think a dialog can only be shown from the main UI thread. So the idea would be to have a handler in your main thread and post a message on it from the new thread, which will launch the dialog.

Gregory
  • 4,384
  • 1
  • 25
  • 21
1

Take a look at AsyncTask

From JavaDocs: AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.

Hussain
  • 5,552
  • 4
  • 40
  • 50
  • ,Thanks but I m feeling hard to know which arguments should be used for async task in my case .could u come up with the structure of async task for my problem. – Randroid Jul 04 '11 at 09:36
  • @Raghu: Take a look at the 4 steps in the above link. U can figure out according to ur process.Also go thro these links for sample. http://jyro.blogspot.com/2009/11/android-asynctask-template.html , http://www.vogella.de/articles/AndroidPerformance/article.html , http://labs.makemachine.net/2010/05/android-asynctask-example/ and http://labs.makemachine.net/2010/05/android-asynctask-example/ – Hussain Jul 04 '11 at 09:47
1

Inside thread you should use Handler for showing the AlertDialog:

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

Use below in place of showdialog(0);

messageHandler.sendEmptyMessage(111);
Vinayak Bevinakatti
  • 40,205
  • 25
  • 108
  • 139
  • ,Thanks but showing message handler cant be resolved when I added the line messageHandler.sendEmptyMessage(111); – Randroid Jul 04 '11 at 09:42
  • errors i m getting like ERROR/AndroidRuntime(1625): java.lang.NullPointerException 07-04 15:30:41.300: ERROR/AndroidRuntime(1625): com.soap.Register$3.onClick(Register.java:116) 07-04 15:30:41.300: ERROR/AndroidRuntime(1625): at android.view.View.performClick(View.java:2408) 07-04 15:30:41.300: ERROR/AndroidRuntime(1625): at android.view.View$PerformClick.run(View.java:8816) 07-04 15:30:41.300: ERROR/AndroidRuntime(1625): at android.os.Handler.handleCallback(Handler.java:587) ERROR/AndroidRuntime(1625): at android.os.Handler.dispatchMessage(Handler.java:92) 0 – Randroid Jul 04 '11 at 10:03