Progress Dialog and Threads

From John Freier
Revision as of 09:13, 27 January 2011 by Jfreier (Talk | contribs)

Jump to: navigation, search

These are nice to use when you need to create a "waiting..." dialog. For example when you make a call to the internet, you would use one, so the user does not have to wait.

One of the issues I ran across was accessing class variables. After alot of research, I figured out, that you can access variable through a class Handler.

private ProgressDialog dialog;
private Handler handlerRefresh = new Handler()
{
     @Override
     public void handleMessage(Message msg)
     {
          Bundle resBundle = (Bundle) msg.obj;
          String question = resBundle.getString("key");
     }
}
dialog = ProgressDialog.show(CurrentActivity.this, "","Please wait...", true);
new Thread(new Runnable()
{
     public void run()
     {
          // Threaded process...
          String name = "John";

          Message myMessage=new Message();
          Bundle resBundle = new Bundle();
          resBundle.putString("key", name);
          myMessage.obj=resBundle;
          handlerRefresh.sendMessage(myMessage);
          dialog.dismiss();
     }
}).start();