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. Unable to run DDMS, failed to get adb version

I'd like to try and run DDMS so I can get some screen captures of my app.
When I try to run ddms.bat, I get this error:

05:38:13 E/adb: Failed to get the adb version: Cannot run program "adb":
rocess error=2, The system cannot find the file specified

I'm running Win 7 x64, and have the latest Eclipse and SDK installed.
Eclipse can launch the emulator and adb just fine.


2. How to get the accesss on /data/misc/vpn folder (Permission denied)?

Hell everyone,

I am working on an applicaiton that requires to access VPN setting
information, howevery I am encountering a permisison denied issue whne
the application accesses VPN setting on /data/misc/vpn folder (Android
2.2 OS), can anyone help how to resolve this issue, did I miss any
users-permission setting in AndroidManifest.xml?

java.io.FileNotFoundException: /data/misc/vpn/profiles/
4599898380687054558.pobj (Permission denied): /data/misc/vpn/profiles/
4599898380687054558.pobj (Permission denied)

Here are the users-permission setting in AndroidManifest.xml now:

    <uses-permission android:name="android.permission.WRITE_SETTINGS" /
android:name="android.permission.WRITE_SECURE_SETTINGS" />

Thanks in andvance,



3. Repeating image on a canvas

4. Start an activity form a class without extending Activity

5. File Retriving

6. List of command

7. Disable button according to spinner item