Android 使用Handler实现Thread间通信

来源:互联网 发布:mysql 修改复合主键 编辑:程序博客网 时间:2024/05/01 11:37

1. 主线程传递消息给自己

public class test3_6  extends Activity implements OnClickListener{
     private Button button;
     private TextView text;
   
    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);
    }

    public void onClick(View v) {
        Handler handler = new MyHandler();
        Message msg = handler.obtainMessage(1, "开开心心每一天!");
        handler.sendMessage(msg);
    }
   
    class MyHandler extends Handler{

        public void handleMessage(Message msg) {
            text.setText((String)msg.obj);
        }
       
    }
}

2. 在主线程中发送消息给其他线程

public class test3_6  extends Activity implements OnClickListener{
     private Button button;
     private TextView text;
     private MyHandler myHandler;
   
    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();

//       不能在myThread.start();之后就立即使用myHandler,此时myHandler还没初始化成功
       
//        Message msg = myHandler.obtainMessage(1, "开开心心每一天!");
//        myHandler.sendMessage(msg);
    }

    public void onClick(View v) {
        Message msg = myHandler.obtainMessage(1, "开开心心每一天!");
        myHandler.sendMessage(msg);
    }

    class MyThread extends Thread{

        public void run() { 
            Looper.prepare();
            myHandler = new MyHandler(Looper.myLooper());
            Looper.loop();
        }

    }
   
    class MyHandler extends Handler{
        public MyHandler(Looper looper) {
            super(looper);
        }

        public void handleMessage(Message msg) {
            System.out.println((String)msg.obj);

//不能在这里调用text.setText((String)msg.obj); 因为它违背了单线程模型:Android UI操作并不是线程安全的并且这些操作必须在UI线程中执行。
        }
       
    }
}

知识点:

1. Android系统中Looper负责管理线程的消息队列和消息循环。

2. 可以通过Loop.myLooper()得到当前线程的Looper对象,通过Loop.getMainLooper()可以获得当前进程的主线程的 Looper对象。

3. 可以通过Looper.prepare()创建消息队列,然后调用Looper.loop()进入消息循环。

4. 在主线程(UI线程)里,如果创建Handler时不传入Looper对象,那么将直接使用主线程(UI线程)的Looper对象(系统已经帮我们创建了);

   在其它线程里,如果创建Handler时不传入Looper对象,那么,这个Handler将不能接收处理消息。

5. Handler的处理过程运行在创建Handler的线程里

6. 一个Looper对应一个MessageQueue

7.  一个线程对应一个Looper

8.  一个Looper可以对应多个Handler

9. 可以实现功能,刷新UI界面。但是这样是不行的,因为它违背了单线程模型:Android UI操作并不是线程安全的并且这些操作必须在UI线程中执行。

new Thread( new Runnable() {    
    public void run() {    
         myView.invalidate();   
     }           
}).start();

3. 给子线程添加消息队列,使得UI线程和子线程护发消息

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

原创粉丝点击