在Android开发中,我们经常需要做一些类似于从网络上下载图片,获取信息等这些耗时的操作,由于Android的anr机制,如果程序5秒钟没有得到响应,程序便会出现异常。由此,我们会想到使用线程来解决这个问题,将耗时的操作放到子线程中,当操作完成后,再更新主线程。但是,问题又来了,在Android中,线程是非安全的,因此不能再子线程中更新UI。因此,Android引入了Handler消息机制,来解决这个问题。
先来简单翻译一下api中对Handler的描述:
Handler允许你去传递和处理Message(消息)和Runnable对象,这个对象对应一个线程的MessageQueue(消息队列)。每个Handler实例对应一个单独的线程以及该线程的消息队列,因此,当你创建了一个Handler,必然也会创建一个线程以及它的消息队列,基于这一点,这个Handler将负责传递message或者Runnable到消息队列中,而当他们
从消息队列中取出时,对他们进行相应的处理。
上述api提到了,Message,MessageQueue这两个概念,Message是一个消息对象,而MessageQueue则是存放Message的消息队列。那么Handler又是如何获取和处理消息的呢?Android引入了一个叫做Looper(环形使者)的类来解决这个问题。
Looper对象用来为一个线程开启一个消息循环,从而操作MessageQueue,在默认情况下,线程不会开启消息循环(message loop),需要调用Looper.onPrepare()初始化一个消息循环,接着创建Handler对象,最后调用Looper.loop()启动线程。但是主线程除外,Android会自动为主线程创建一个Looper对象,从而开启消息循环。
说了这么多,最后直接上代码:
MainActivity.java
<span style="font-size:14px;">package com.example.andorid_handler;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
private Handler handler,handler2;
private Button button1,button2,button3;
private TextView textView1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
textView1 = (TextView) findViewById(R.id.textView1);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
MyThread thread = new MyThread();
thread.start();
}
});
button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Thread myRunnable = new Thread(new MyRunnable());
myRunnable.start();
}
});
button3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
looperThread looper = new looperThread();
looper.start();
}
});
handler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.what == 1) {
textView1.setText("继承thread");
}
else if(msg.what == 2){
textView1.setText("实现runnbale");
}
}
};
}
public class MyThread extends Thread {
@Override
public void run() {
// TODO Auto-generated method stub
Message message = Message.obtain();
message.what = 1;
handler.sendMessage(message);
}
}
public class MyRunnable implements Runnable {
@Override
public void run() {
// TODO Auto-generated method stub
Message message = Message.obtain();
message.what = 2;
handler.sendMessage(message);
}
}
public class looperThread extends Thread{
@Override
public void run() {
// TODO Auto-generated method stub
Looper.prepare();
handler2 = new Handler(){
public void handleMessage(Message msg){
if(msg.what == 1)
Log.i("Looper","使用了looper!");
}
};
Message message = Message.obtain();
message.what = 1;
handler2.sendMessage(message);
Looper.loop();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
</span>
activity_main.xml
<span style="font-size:14px;"><RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="继承Thread" />
<Button
android:id="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:text="实现Runnable" />
<Button
android:id="@+id/button3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button2"
android:layout_below="@+id/button2"
android:text="looper" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button3"
android:layout_centerHorizontal="true"
android:layout_marginTop="109dp"
android:text="@string/hello_world" />
</RelativeLayout></span>
截图: