TWiki> CS422 Web>WebLeftBar (revision 6)EditAttach
Please refer this for help :

Android provides Handler and Looper for threads to communication with each other. . Please dont forget to override run method inside thread.

When a Handler is created, it’s bound to the message queue of the thread that created it. If you create it in the main thread, you don’t need any extra code to loop the message queue for the main thread since it’s already been started when you run your application. However, if you are creating a Handler in a child thread, you need to initialize the thread to listen to its message queue before creating the Handler.

class ChildThread extends Thread {

    public void run() {

        /*
         * You have to prepare the looper before creating the handler.
         */
        Looper.prepare();

        /*
         * Create the child handler on the child thread so it is bound to the
         * child thread's message queue.
         */
        mChildHandler = new Handler() {

            public void handleMessage(Message msg) {

                /*
                 * Do some expensive operations there.
                 */
            }
        };

        /*
         * Start looping the message queue of this thread.
         */
        Looper.loop();
    }
}

Whenever the Handler receives a message, it would run the handleMessage(…). You can do some expensive operations in there. For example, you need to constantly send some data to the server. It probably would be more efficient if you have a thread listening for the messages to do the job instead of creating and running a new thread each time you need to do so.

If you are done with the looper, don’t forget to stop it by using its quit() method. For example:

mChildHandler.getLooper().quit();
Hope it will be helpful to everyone....
- Sugi.. 
Edit | Attach | Print version | History: r7 < r6 < r5 < r4 < r3 | Backlinks | Raw View | Raw edit | More topic actions...
Topic revision: r6 - 2010-05-02 - 20:49:50 - Main.svenug2
 
Copyright 2016 The Board of Trustees
of the University of Illinois.webmaster@cs.uic.edu
WISEST
Helping Women Faculty Advance
Funded by NSF