5

I am learning via a book and it gives me this example:

Handler handler=new Handler() { 
    @Override 
    public void handleMessage(Message msg) { 
        bar.incrementProgressBy(5); 
    } 
}; 

and

Thread background=new Thread(new Runnable() { 
    public void run() { 
        try { 
            for (int i=0;i<20 && isRunning.get();i++) { 
                Thread.sleep(500); 
                handler.sendMessage(handler.obtainMessage()); 
            } 
        } catch (Throwable t) { 
            // just end the background thread 
        } 
    } 
}); 

Which works out great. But, further down in the book it says:

If you would rather not fuss with Message objects, you can also pass Runnable objects to the Handler, which will run those Runnable objects on the activity UI thread. ...you can use those same methods on any View (i.e., any widget or container). This slightly simplifies your code, in that you can then skip the Handler object.

But there are no examples given of how to do this via a Runnable object. Does anyone have an example?

Allan Pereira
  • 2,572
  • 4
  • 21
  • 28
Ryan
  • 9,821
  • 22
  • 66
  • 101

2 Answers2

5

Something like this:

Handler h = new Handler();

Thread background=new Thread(new Runnable() { 
          public void run() { 
            try { 
              for (int i=0;i<20 && isRunning.get();i++) { 
                Thread.sleep(500); 
                handler.post(new Runnable() {
                  public void run() {
                    bar.incrementProgressBy(5);
                  }
                });
              } 
            } 
            catch (Throwable t) { 
              // just end the background thread 
            } 
          } 
        }); 
Alex Gitelman
  • 24,429
  • 7
  • 52
  • 49
  • Thanks for the example though, have selected your answer – Ryan Jul 24 '11 at 04:33
  • You have two runnables, now. Therefore, you will have two run() methods. Not much we can do about it... (But some code structuring can make it look slightly nicer.) – Alex Gitelman Jul 24 '11 at 04:38
  • Still looks a bit weird to me :) As a person who is used to this (ie, not a noob like me)... is this a popular format for Android developers? Do you use something like this much in your code? – Ryan Jul 24 '11 at 04:43
  • Nested classes are often used. `Runnable` in particular. I think it's more specific to Java (UI in particular) rather than Android. – Alex Gitelman Jul 24 '11 at 04:47
  • Cool, thanks for the example and taking the time to answer my Qs. Cheers! – Ryan Jul 24 '11 at 04:54
  • What does exactly mean that we can "skip the Handler object." using `Runnable`? Do not we need `Handler` object for passing `Runnable`? – GVillani82 Mar 22 '13 at 22:59
  • @Joseph82: Maybe it's referring to what's written at the end of section 5.2 of this tutorial: http://www.vogella.com/articles/AndroidBackgroundProcessing/article.html (I haven't tried it myself, just starting to learn Android programming.) – RenniePet Jul 31 '13 at 01:15
2

As per the Android Docs for Handler:

public final boolean post (Runnable r)

Since: API Level 1 Causes the Runnable r to be added to the message queue. The runnable will be run on the thread to which this handler is attached. Parameters

r -- The Runnable that will be executed. Returns

Returns true if the Runnable was successfully placed in to the message queue. Returns false on failure, usually because the looper processing the message queue is exiting.

nicholas.hauschild
  • 42,483
  • 9
  • 127
  • 120
  • Finding the definitions are pretty easy via a google or docs search. But for a noob like me those definitions could be explaining String Theory or rocket science, thats why I was asking if someone could post an example please? It's easier to understand after seeing the code and then matching it up to the definition. – Ryan Jul 24 '11 at 04:18