Difference between revisions of "Progress Dialog and Threads"

From John Freier
Jump to: navigation, search
(Created page with ' private Handler handlerRefresh = new Handler() { @Override public void handleMessage(Message msg) { // Code to process the response and update UI. …')
 
 
(7 intermediate revisions by the same user not shown)
Line 1: Line 1:
private Handler handlerRefresh = new Handler()
+
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.
{
+
    @Override
+
    public void handleMessage(Message msg)
+
    {
+
          // Code to process the response and update UI.
+
          Bundle resBundle = (Bundle) msg.obj;
+
          String question = resBundle.getString(BUNDLE_QUESTION);
+
    }
+
  }
+
   
+
  
 +
One of the issues I ran across was accessing class variables.  After a lot of research, I figured out, that you can access variable through a class Handler.  You pass variable to the handler through the messages.
  
...
+
Example:
  
dialog = ProgressDialog.show(CurrentActivity.this, "","Posting to twitter...", true);
+
Create a private class variable.
        new Thread(new Runnable()
+
private ProgressDialog dialog;
        {
+
    public void run()
+
    {
+
    TwitterPost.Post(getBaseContext(), tweetString);
+
   
+
    Message myMessage=new Message();
+
    Bundle resBundle = new Bundle();
+
    resBundle.putString(BUNDLE_USER_ANSWER, tweetString);
+
    myMessage.obj=resBundle;
+
    handlerTwitterPost.sendMessage(myMessage);
+
    dialog.dismiss();
+
    }
+
        }).start();
+
  
...
+
This is the handler so that you can access class variables. example: text boxes, images...
 +
private Handler handlerRefresh = new Handler()
 +
{
 +
      @Override
 +
      public void handleMessage(Message msg)
 +
      {
 +
          Bundle resBundle = (Bundle) msg.obj;
 +
          String name= resBundle.getString("key");
 +
      }
 +
}
  
private ProgressDialog dialog;
+
This is the thread creation, it will show the dialog and process in the background.
 +
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();

Latest revision as of 09:19, 27 January 2011

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 a lot of research, I figured out, that you can access variable through a class Handler. You pass variable to the handler through the messages.

Example:

Create a private class variable.

private ProgressDialog dialog;

This is the handler so that you can access class variables. example: text boxes, images...

private Handler handlerRefresh = new Handler()
{
     @Override
     public void handleMessage(Message msg)
     {
          Bundle resBundle = (Bundle) msg.obj;
          String name= resBundle.getString("key");
     }
}

This is the thread creation, it will show the dialog and process in the background.

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