首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

怎么使用Handler实现主线程往子线程传递消息

2014-01-03 
如何使用Handler实现主线程往子线程传递消息myThread new Thread(new Runnable() {@Overridepublic void

如何使用Handler实现主线程往子线程传递消息


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("", "这是消息循环开始之后的代码");
}
}
});

如以上代码,如何能让Looper.loop();之后的代码执行呢?也就是说loop之后,不影响线程逻辑的执行~~~~求思路
[解决办法]
Looper.loop(); 之后 需要做啥?

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);

[解决办法]
楼主牛逼,这啥需求?语句执行到Looper.loop();后,线程会阻塞在这里,后面的语句根本无法执行。
除非让loop()消息循环退出(可以调用quit()函数),否则根本无法按照你的需求来实现。
/**
     * 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();
        }
    }



[解决办法]


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);就可以执行了。

[解决办法]
引用:
就是说能不能实现这种效果,某个运行的线程里,可以随时接收或者监听其他线程发来的消息~~~~

怎么不可以??
你的线程里不是实例化了一个个Handler对象handler么?在其他线程(比如主线程)中,调用handler.sendEmptyMessage()不就接收了其他线程发来的消息么?


[解决办法]
哥们看下 这篇文章或许会帮到你。

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

写在Looper.loop()之后的代码不会被执行,这个函数内部是一个循环,当调用mHandler.getLooper().quit()后,loop才会中止,其后的代码才能得以运行。

热点排行