Android HandlerThread使用小例

之前研究过handler 和 looper 消息队列,不过Android里的handler不是另外开启线程来执行的,还是在主UI线程中,如果想另启线程的话需要用到HandlerThread来实现。在使用HandlerThread的时候需要实现CallBack接口以重写handlerMessage方法,在handlerMessage方法中来处理自己的逻辑。下来给出一个小例子程序。

layout文件很简单,就一个按钮来启动HanlderTread线程

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"  
    android:orientation="vertical" >  
  
    <TextView  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:text="@string/hello" />  
  
    <Button  
        android:id="@+id/handlerThreadBtn"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:text="startHandlerThread" />  
  
</LinearLayout>  
Activity代码如下:

package com.tayue;  
  
import android.app.Activity;  
import android.os.Bundle;  
import android.os.Handler;  
import android.os.Handler.Callback;  
import android.os.HandlerThread;  
import android.os.Message;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.widget.Button;  
/** 
 *  
 * @author xionglei 
 * 
 */  
public class TestHandlerActivity extends Activity implements OnClickListener{  
      
    public Button handlerThreadBTN;   
    MyHandlerThread handlerThread;  
    Handler handler;  
      
    /** Called when the activity is first created. */  
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        //打印UI线程的名称   
        System.out.println("onCreate  CurrentThread = " + Thread.currentThread().getName());  
          
        setContentView(R.layout.main);  
          
        handlerThreadBTN = (Button) findViewById(R.id.handlerThreadBtn);  
        handlerThreadBTN.setOnClickListener(this);  
          
        handlerThread = new MyHandlerThread("myHanler");  
        handlerThread.start();  
        //注意: 这里必须用到handler的这个构造器,因为需要把callback传进去,从而使自己的HandlerThread的handlerMessage来替换掉Handler原生的handlerThread   
        handler = new Handler(handlerThread.getLooper(), handlerThread);  
          
          
    }  
  
    @Override  
    public void onClick(View v) {  
        //点击按钮后来开启线程   
        handler.sendEmptyMessage(1);  
    }  
      
      
    private class MyHandlerThread extends HandlerThread implements Callback {  
  
        public MyHandlerThread(String name) {  
            super(name);  
        }  
  
        @Override  
        public boolean handleMessage(Message msg) {  
            //打印线程的名称   
            System.out.println(" handleMessage CurrentThread = " + Thread.currentThread().getName());  
            return true;  
        }  
          
          
    }  
      
      
      
      
}  
点击按钮,打印的日志如下(这里点击了3次)
07-06 09:32:48.776: I/System.out(780): onCreate  CurrentThread = main
07-06 09:32:55.076: I/System.out(780):  handleMessage CurrentThread = myHanler
07-06 09:32:58.669: I/System.out(780):  handleMessage CurrentThread = myHanler
07-06 09:33:03.476: I/System.out(780):  handleMessage CurrentThread = myHanler

 HandlerThread就这么简单。

 当然 android自己也有异步线程的handler,就是AsyncTask,这个类就是封装了HandlerThread 和handler来实现异步多线程的操作的。

你可能感兴趣的:(handler,ThreadHandler)