1

As shown in the code below, if a Progressdialoge exists, I want to set a new message to it, and dismiss it after 3 seconds.

In the run-time, what happens is, the old message remains set to the progressdialog and lasts for 3 seconds and then the progressdialog dismisses without the new message is set. Any suggestions?

Code:

if ( (location != null) && (mProgreeDialoge != null) ){
            mProgreeDialoge.setMessage(PROGRESS_DIALOGUE_MSG01);
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

Update:

if ( (location != null) && (mProgreeDialoge != null) ){
            mProgreeDialoge.setMessage(PROGRESS_DIALOGUE_MSG01);
            new Handler().postDelayed(new Runnable() {

                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    mProgreeDialoge.dismiss();
                }
            }, FREEZE_PROGRESS_DIALOGUE_TIME);
Amrmsmb
  • 1
  • 27
  • 104
  • 226

1 Answers1

0

I would use a Handler for this, and have it post the message delayed with 3000 milliseconds.

Below is a simple Handler implementation, you can extend/customize it with int arguments and the obj member too:

private final Handler mHandler = new Handler()
{
    @Override
    public void handleMessage(Message msg)
    {
        super.handleMessage(msg);
        if (mProgreeDialoge.isShowing())
            mProgreeDialoge.dismiss();
    }
};

Then after showing the progress dialog, just send a message to the handler with a delay of 3 seconds:

mProgreeDialoge.setMessage(PROGRESS_DIALOGUE_MSG01);
mHandler.sendMessageDelayed(new Message(), 3000);
rekaszeru
  • 19,130
  • 7
  • 59
  • 73
  • In concept of using handlers, it worked now for me, but in a bit different way than yours. please have a look at the update – Amrmsmb May 19 '14 at 21:35
  • @Amr, sure, if you don't want to reuse the handler, or examine it's members (`arg1`, `arg2`, `obj`), it's simpler to not extend it (though you need to create a `Runnable` to run on a different thread. Glad you made it! – rekaszeru May 19 '14 at 21:40