Looper/Handler issue

by BD » Sat, 10 Oct 2009 14:22:07 GMT

Sponsored Links
I'd like to know if Looper can enable handler to handle one message at
a time, instead of handling all the messages in the queue together in
one blocking call when Looper.loop() is called.

Because the looper sends all the messages in the queue to the handler
one after the other in one blocking call, my application displays the
ANR message. Instead, I want to be able to handle one single message,
one blocking call at a time - the instant it falls into the queue.

The following code illustrates my problem: (pls note the lines
referred: 1,2,3,4&5)
Question 1: After calling looper.loop in line3, line4 and 5 don't run.
Why is this? I tried quit(), but it doesn't help.
Question 2: Instead of displaying 0,1,2,3,4 in one blocking call (at
line3), I'd like to have 5 different blocking calls for each.
Essentially (sorry, if i sound repetitive), I want to be able to use
looper in such a way that each time, it ensures only one message gets
handled. Is there a way to do this?

I'd appreciate any thoughts on this.
Thanks for the help!!

public class TestLoop extends Activity implements Runnable {
        private Thread bkdg;

        private Handler handler = new Handler() {

                public void handleMessage(Message msg) {
1)                      Log.i("Bkdg thread", "Message what = " + msg.what);


    public void onCreate(Bundle savedInstanceState) {

        bkdg = new Thread(this);


        public void run() {
                int i = 0;
                while(i < 5) {
2)                      handler.sendEmptyMessage(i);
3)              Looper.loop();
4)              handler.sendEmptyMessage(5);
5)              Log.i("bkdg thread", "reached here");


Looper/Handler issue

by Dianne Hackborn » Sat, 10 Oct 2009 14:35:23 GMT

 That is simply not how Looper works.  You call Looper.loop() to have the
thread enter the message looper, and process messages until it is time for
the thread to exit.
Specific comments:

The following code illustrates my problem: (pls note the lines

Where did you try quit()?  A looper continues running its loop until you
tell the looper to quit.

Also note that in the code you have included, your 'handler' is associated
with the main thread (since it was constructed from the main thread), not
the other thread you are creating.  You haven't made any handlers to the
secondary thread, so have never sent a message to it, so it has just sat in
the loop and done nothing.

Question 2: Instead of displaying 0,1,2,3,4 in one blocking call (at

You can't.


Dianne Hackborn
Android framework engineer

Note: please don't send private questions to me, as I don't have time to
provide private support, and so won't reply to such e-mails.  All such
questions should be posted on public forums, where I and others can see and
answer them.


Sponsored Links

Looper/Handler issue

by BD » Sun, 11 Oct 2009 11:49:14 GMT

 Thanks Dianne, for the reply. I fixed it by shifting the handler to
the background thread, and including the looper quit() inside the


Other Threads

1. display frame buffer

Hi, I have a RGB565 frame buffer. I need to display it on a screen. I
am not finding a suitable API to pass a frame buffer [unsigned char*]
to display on a screen. How can i display my own frame buffer on a
screen ?


2. how to display hebrew characters


I need to display hebrew characters.  I tried the simplest thing I
could think of:

tv.setText("Hi :\u05d0:");

but, all I see are [] in place of the characters.



3. Problem with sockets and UI

4. Clone Vendor folder

5. MyLocationOverlay and orientation change

6. using main.xml and additional things?

7. combining two view options