现在的位置: 首页 > 综合 > 正文

主线程接收子线程数据

2018年03月31日 ⁄ 综合 ⁄ 共 3387字 ⁄ 字号 评论关闭

 

在开发之中子线程都是完成一些比较浪费时间的操作,这些操作不应涉及显示的操作,而只是涉及到数据的操作。

 

 

在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) {

         switch(msg.what){

         case SETMAIN:

            MyThreadDemo.this.msg.setText("主线程接收数据:" +

         msg.obj.toString());

         }

       }

        };

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

              Message toMain = MyThreadDemo.this.mainHandler.obtainMessage();

              toMain.obj = "这是子线程发送给主线程的信息" + super.getLooper()

                   .getThread().getName();

              toMain.what = SETMAIN;

              MyThreadDemo.this.mainHandler.sendMessage(toMain);

              break;

            }

         }

       };

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

     }

    }

  @Override

  protected void onDestroy() {  //复写onDestroy方法以收尾

     super.onDestroy();

     MyThreadDemo.this.childHandler.getLooper().quit(); //结束子线程的操作队列

  }

}

 

 

抱歉!评论已关闭.