介绍:
HandlerThread继承Thread,当线程开启时,也就是它run方法运行起来后,线程同时创建了一个含有消息队列的Looper,并对外提供自己这个Looper对象的get方法,这就是它和普通Thread唯一不同的地方。
好处:
为什么要使用HandlerThread
1、开发中如果多次使用类似new Thread(){}.start(); 这种方式开启一个子线程,会创建多个匿名线程,使得程序运行越来越慢,而HandlerThread自带Looper使他可以通过消息来多次重复使用当前线程,节省开支
2、android系统提供的Handler类内部的Looper默认绑定的是UI线程的消息队列,对于非UI线程又想使得消息机制,那么HandlerThread内部的Looper是最合适的,它不会干扰或阻塞UI线程
用法:
HandlerThread既然本质是Thread,为何前面加了一个Handler?android中Handler类本质上就是从它内部的Looper中不断取消息,然后触发它内部的Callback接口的handleMessage方法,让用户去实现对消息的具体处理。而HandlerThread本身自带Looper,只要它实现了Callback接口,那么HandlerThread也可以在自己线程内处理线程发出的消息,充分实现非UI线程中较低开支下的消息处理。
总结:
1、HandlerThread继承Thread,与Thread不同就是提供自己的Looper对象,不过需要先启动这个HandlerThread线程
2、调用handlerThread.getLooper()来获取Looper对象,则不要调用Looper.prepare()和Looper.loop()方法
要更好的理解Looper以及HandlerThread,先查看Android UI编程(5)——Looper
AndroidManifest.xml——没有做任何修改,创建工程默认生成的
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.wxl.handlerthread"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.wxl.handlerthread.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发送hello world"/>
</LinearLayout>
MainActivity.java
package com.wxl.handlerthread;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.app.Activity;
public class MainActivity extends Activity {
private TextView textView;
private Button button;
private Handler handler;
private MyThread myThread;
private HandlerThread handlerThread;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView)this.findViewById(R.id.textView);
button = (Button)this.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Message message = Message.obtain();
message.what = 1;
message.obj = "hello world";
handler.sendMessage(message);
}
});
myThread = new MyThread();
handlerThread = new HandlerThread("");
myThread.start();
handlerThread.start();
}
public class MyThread extends Thread {
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
Looper.prepare();
handler = new Handler(handlerThread.getLooper()){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
if (1 == msg.what)
{
//textView.setText(""+msg.obj);子线程不能更新UI
Log.i("", ""+msg.obj);
}
}
};
Looper.loop();
}
}
}
点击按钮: