2014年1月2日星期四

[Android] How to use Handler to achieve the main thread to the child thread passing messages


myThread = new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
if (handler == null) {
Looper.prepare();
handler = new Handler(){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
}
};
myHandler.sendEmptyMessage(1);
Looper.loop();
Log.e("", "这是消息循环开始之后的代码");
}
}
});

As the above code , how can Looper.loop (); code execution after it ? That after the loop, the thread does not affect the execution logic ~ ~ ~ ~ seeking ideas
------ Solution ---------------------- ----------------------
Looper.loop (); then need Zuosha ?

Lopper myLopper = null;
new Thread () {
public void run () {
Looper.prepare ();
myLopper = Lopper.myLooper ();
Looper.loop ();
}
} start ().;

class Threadhandler extends Handler {
public void handleMessage (Message msg) {

}

}
Threadhandler handler = new Threadhandler (myLopper);

------ Solution ------------------------------------ --------
landlord regressed , what it needs ? Statement is executed to Looper.loop (); later , the thread will be blocked here , behind the statement could not execute .
Unless let loop () message loop exits ( you can call the quit () function ) , or simply can not be achieved in accordance with your needs .
/**
     * Run the message queue in this thread. Be sure to call
     * {@link #quit()} to end the loop.
     */
    public static void loop() {
        final Looper me = myLooper();
        if (me == null) {
            throw new RuntimeException("No Looper; Looper.prepare() wasn't called on this thread.");
        }
        final MessageQueue queue = me.mQueue;

        // Make sure the identity of this thread is that of the local process,
        // and keep track of what that identity token actually is.
        Binder.clearCallingIdentity();
        final long ident = Binder.clearCallingIdentity();

        for (;;) {
            Message msg = queue.next(); // might block
            if (msg == null) {
                // No message indicates that the message queue is quitting.
                return;
            }

            // This must be in a local variable, in case a UI event sets the logger
            Printer logging = me.mLogging;
            if (logging != null) {
                logging.println(">>>>> Dispatching to " + msg.target + " " +
                        msg.callback + ": " + msg.what);
            }

            msg.target.dispatchMessage(msg);

            if (logging != null) {
                logging.println("<<<<< Finished to " + msg.target + " " + msg.callback);
            }

            // Make sure that during the course of dispatching the
            // identity of the thread wasn't corrupted.
            final long newIdent = Binder.clearCallingIdentity();
            if (ident != newIdent) {
                Log.wtf(TAG, "Thread identity changed from 0x"
                        + Long.toHexString(ident) + " to 0x"
                        + Long.toHexString(newIdent) + " while dispatching to "
                        + msg.target.getClass().getName() + " "
                        + msg.callback + " what=" + msg.what);
            }

            msg.recycle();
        }
    }


------ Solution ------------------------------------ --------


myThread = new Thread(new Runnable() {
            @Override
            public void run() {
                // TODO Auto-generated method stub
                if (handler == null) {
                    Looper.prepare();    
                    handler = new Handler(Looper.myLooper()){//这里加上
                        @Override
                        public void handleMessage(Message msg) {
                            // TODO Auto-generated method stub
                            super.handleMessage(msg);
                            switch(msg.what){
                            case do_some_th:break;
                            }
                        }
                    };
                    myHandler.sendEmptyMessage(1);
                    Looper.loop();
                    Log.e("", "这是消息循环开始之后的代码");
                }
            }
        });

//主线程给子线程发消息:
在主线程里面调用handler.sendEmptyMessage(do_some_th);就可以执行了。

------ Solution -------------------------- ------------------

how not ? ?
your thread is not instantiated one Handler object handler it? In other threads ( such as the main thread ) , call handler.sendEmptyMessage () is not on the other thread receives a message sent to it ?
------ Solution ---------------------------------------- ----
buddy facie this article might help you.

http://blog.csdn.net/heng615975867/article/details/9194219

write after ( ) in Looper.loop code will not be executed , this internal function is a cycle , when the call mHandler.getLooper (). quit (), loop will abort , then the code can be operation.

------ eference ------------------------------------ ---


eggs hurt the landlord - also known loop inside a loop , there is news on the treatment, no message hangs , so I want to ask you what there is no solution to , Quxianjiuguo also good ~ ~ ~ because used a third-party framework , his logic in a separate thread , but logic is been executed , that as long as the program is running , the thread running state , the landlord wanted to in the main thread message to this thread ~ ~ ~ the results showed that the above problems can not initialize handler, I do not know how to get a
------ eference ----------------------- ----------------
That can not achieve this effect, a running thread , ready to receive or monitor messages sent by other threads ~ ~ ~ ~
------ eference --------------------------------------- < br>

ah, I want Log.e ("", " This is the code after the start of the news cycle " ) ; sentence execution , rather than do_some_th Oh , the other can not quit () looper because after the withdrawal of this thread can not continue to monitor messages sent to the main thread of the
------ eference ----------------------- ----------------

how not ? ?   
your thread is not instantiated one Handler object handler it? In other threads ( such as the main thread ) , call handler.sendEmptyMessage () is not on the other thread receives a message sent to it ?  
must be called after instantiation Looper.loop (); handler to receive the message sent by the other thread , but Looper.loop (); There is a cycle , will lead Looper.loop (); code after not execution , so I put Looper.loop (); logic can not be performed after
------ eference --------------------- ------------------
already use other programs to address the needs, thank you ~ ~

没有评论:

发表评论