Looper/Handler issue

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


Sponsored Links
 Hi.
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() {

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

        };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        bkdg = new Thread(this);
        bkdg.start();

    }

        public void run() {
                Looper.prepare();
                int i = 0;
                while(i < 5) {
2)                      handler.sendEmptyMessage(i);
                        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.



No.

-- 
Dianne Hackborn
Android framework engineer
hack...@android.com

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
handler.



--~--~---------~--~----~------------~-------~--~----~



Other Threads

1. sqlite illeagal state exception unable to close due unfinallised statements.

Hi all

I am developing one database app. in which i am getting exception
named as unable to close database due to unfinallised statements, and
throwing finallized cursor becoz cursor is not deactivated or closed.
And beauty of the exception its not getting when i am doing it on
emulator but on some of the phones. reports coming from android
market.  please tell me hoe can i solve this problem.

details of messages.(this details are coming in reports from market
not on emulator therefore i am unable to trace it.)

android.database.sqlite.SQLiteException: unable to close due to
unfinalised statements
at android.database.sqlite.SQLiteDatabase.dbclose(Native Method)
at
android.database.sqlite.SQLiteDatabase.onAllReferencesReleased(SQLiteDatabase.java:
322)
at android.database.sqlite.SQLiteDatabase.close(SQLiteDatabase.java:
990)
at
android.database.sqlite.SQLiteOpenHelper.close(SQLiteOpenHelper.java:
191)
at
com.dexterltd.leave_no_trace_lite.DataManager.close(DataManager.java:
185)
at
com.dexterltd.leave_no_trace_lite.ContactsSettings.getContactSettings(ContactsSettings.java:
657)
at com.dexterltd.leave_no_trace_lite.ContactsSettings.access
$9(ContactsSettings.java:529)
at com.dexterltd.leave_no_trace_lite.ContactsSettings
$4.run(ContactsSettings.java:127)

-- 

2. [WTB] SE X10 Mini Pro

Permisi,suhu2 semua.
Nyari SE X10 MP yg BNIB nih..
Pengen ngasih hadiah sm nyokap hehe. Kalo bisa yg harga gila atau harga
setengah gila.
Posisi di jkt-bekasi,pengen CODan aja deh biar cepet.
Langsung aja kasih penawaran ke email ini ya. Tq. :)

-- 
===============
"

3. How can i implement geochat in android application?

4. display columns name at the top of list.

5. minta build.prop miui for HD2

6. HTC Desire HD vs Google Nexus S

7. is it possible to play flash in videoview?