主线程向子线程发送信息

时间:2022-10-25 18:28:29

主线程向子线程发送信息

 

在main.xml中:

 

<LinearLayout

    xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:gravity="center_horizontal"

    android:background="#000000"

    android:orientation="vertical">

    <TextView

        android:id="@+id/msg"

        android:gravity="center_horizontal"

        android:layout_marginTop="8dp"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:textSize="20dp"

    android:textColor="#ffffff"

        android:text="等待子线程发送信息"/>

    <Button

        android:id="@+id/but"

        android:layout_marginTop="18dp"

        android:layout_width="80dp"

        android:layout_height="40dp"

        android:background="#3399ff"

        android:textColor="#ffffff"

        android:text="进行交互"/>

</LinearLayout>

 

 

 

 

 

在MyThreadDemo.java中:

 

package com.li.threadproject;

 

import android.os.Bundle;

import android.os.Handler;

import android.os.Looper;

import android.os.Message;

import android.app.Activity;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.TextView;

import android.support.v4.app.NavUtils;

 

public class MyThreadDemo extends Activity {

  public static final int SETMAIN = 1; //设置一个what的标记

  public static final int SETCHILD = 2; //设置一个what的标记

  private Handler mainHandler,childHandler;

  private TextView msg = null;

  private Button but = null;

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        super.setContentView(R.layout.main);

        this.msg = (TextView)super.findViewById(R.id.msg);

        this.but = (Button)super.findViewById(R.id.but);

        this.mainHandler = new Handler(){

 

       @Override

       public void handleMessage(Message msg) {

         super.handleMessage(msg);

       }

        };

        new Thread(new ChildThread(),"Child Thread").start();

        this.but.setOnClickListener(new OnClickListenerImpl());

    }   

    private class OnClickListenerImpl implements OnClickListener{

     public void onClick(View v) { //将信息发送到子线程中

       if(MyThreadDemo.this.childHandler !=null){

       Message childMsg = MyThreadDemo.this.childHandler

            .obtainMessage();    //创建消息

       childMsg.obj = MyThreadDemo.this.mainHandler.getLooper()

            .getThread().getName() + "Hello 李叶文";

       childMsg.what = SETCHILD;

       MyThreadDemo.this.childHandler.sendMessage(childMsg);

       }

     }

    }

    class ChildThread implements Runnable{   //子线程

     public void run() {

       Looper.prepare();

       MyThreadDemo.this.childHandler = new Handler(){

         @Override

         public void handleMessage(Message msg) {

            switch(msg.what){

            case SETCHILD:     //子线程接收主线程发送来的消息

              System.out.println("### Main Child Message:" + msg.obj);

            }

         }

       };

       Looper.loop();     //创建消息队列

     }

    }

}