我们知道自己创建的子线程默认是没有消息队列的,不能接受消息,所以我研究了下别人的一个程序,此程序的本意是给自己创建的子线程添加消息队列,点击按钮之后UI线程发消息给子线程,子线程收到消息再发一个消息给UI线程。但是我运行了一下,并没有按照预期输出相关信息,目前存在的问题是点击按钮之后,UI线程给子线程发送的消息,子线程并没有接受到,导致不能交互。那么以上代码哪里出了问题呢,我把完整的源码也附上,请求高手解答下。package com.WriteCode.AndroidMessageQueue;import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;public class AndroidMessageQueue extends Activity {

private static final String TAG = "MainThread";
private Handler mMainHandler, mChildHandler;
private TextView info;
private Button msgBtn;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        info = (TextView)findViewById(R.id.info);
        msgBtn = (Button)findViewById(R.id.msgBtn);
        
        mMainHandler = new Handler()
        {
         @Override
         public void handleMessage(Message msg)
         {
         Log.e(TAG, "Got an incoming message from the child thread - " + (String)msg.obj);
         info.setText((String)msg.obj);
         }
        };
        
        new ChildThread().start();
        
        msgBtn.setOnClickListener(new OnClickListener()
        {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (mChildHandler != null)
{
Message childMsg = mChildHandler.obtainMessage();
childMsg.obj = mMainHandler.getLooper().getThread().getName() + " says Hello";         //main says Hello
mChildHandler.sendMessage(childMsg);
Log.e(TAG, "Send a message to the child thread - " + (String)childMsg.obj);
}
}
        
        });
    }
    
    
    public void onDestroy()
    {
     super.onDestroy();
     Log.e(TAG, "Stop looping the child thread's message queue");
     mChildHandler.getLooper().quit();
    }
    
    class ChildThread extends Thread
    {
     private static final String CHILD_TAG = "ChildThread";
    
     public void run()
     {
     this.setName("ChildThread");
    
     //在Handler创建之前需要初始化消息队列
     Looper.prepare();
    
     mChildHandler = new Handler()
     {
     @Override
     public void handleMessage(Message msg)
     {
     Log.e(CHILD_TAG, "Got an incoming message from the main thread - " + (String)msg.obj);
     try
     {
     sleep(100);
     Message toMain = mMainHandler.obtainMessage();
     toMain.obj = "This is " + this.getLooper().getThread().getName() + ". Did you send me \"" + (String)msg.obj + "\"?";
     mMainHandler.sendMessage(toMain);
     Log.e(CHILD_TAG, "Send a message to the main thread - " + (String)toMain.obj);
     }
     catch(Exception e)
     {
     e.printStackTrace();
     }
    
     Log.e(CHILD_TAG, "Child handler is bound to - " + mChildHandler.getLooper().getThread().getName());
    
     //启动子线程消息循环队列
     Looper.loop();
     }
     };
     }
    }
}

解决方案 »

  1.   

    把mChildHandler = new Handler()放到线程外,也就是你的主类里,再试试。
      

  2.   

    这个应该能解决你的问题。handler与多线程消息处理
    http://www.blogjava.net/lihao336/archive/2010/11/29/339316.html
      

  3.   

    我是这样做的,可以实现功能!希望有用!O(∩_∩)O~
    package com.android.test3;import android.app.Activity;
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.Looper;
    import android.os.Message;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.TextView;public class test3_6  extends Activity implements OnClickListener{
     private Button button;
     private TextView text;
     private MyHandler myHandler;
     private MainHandler mainHandler;

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

            button = (Button)findViewById(R.id.button);
            text = (TextView)findViewById(R.id.text);
            
        button.setOnClickListener(this); 
        
        //创建一个线程
        MyThread myThread = new MyThread();
        myThread.start(); }

    public void onClick(View v) {
    if(myHandler != null){
    Message msg = myHandler.obtainMessage(1, "开开心心每一天!");
    myHandler.sendMessage(msg);
    }
    } class MainHandler extends Handler{
    public MainHandler(Looper looper) {
    super(looper);
    }
    public void handleMessage(Message msg) {
    text.setText((String)msg.obj);
    }

    }

    class MyThread extends Thread{ public void run() {
    //实例化mainHandler
    mainHandler = new MainHandler(getMainLooper());
    //实例化myHandler
    Looper.prepare();
    myHandler = new MyHandler(Looper.myLooper());
    Looper.loop();
    } }

    class MyHandler extends Handler{
    public MyHandler(Looper looper) {
    super(looper);
    } public void handleMessage(Message msg) {
    if(mainHandler != null){
    Message msg2 = mainHandler.obtainMessage(1, msg.obj);
    mainHandler.sendMessage(msg2);
    }
    }

    }
    }http://blog.csdn.net/z496844387/archive/2011/05/12/6414261.aspx