Android中的消息机制

时间:2023-11-28 15:20:38

在分析Android消息机制之前。我们先来看一段代码:

public class MainActivity extends Activity implements View.OnClickListener {  

    private TextView stateText;
private Button btn; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
stateText = (TextView) findViewById(R.id.tv);
btn = (Button) findViewById(R.id.btn); btn.setOnClickListener(this);
} @Override
public void onClick(View v) {
new WorkThread().start();
} //工作线程
private class WorkThread extends Thread {
@Override
public void run() {
//......处理比較耗时的操作 //处理完毕后改变状态
stateText.setText("completed");
}
}
}

这段代码似乎看上去非常正常,可是当你执行时就会发现。它会报一个致命性的异常:

ERROR/AndroidRuntime(421): FATAL EXCEPTION: Thread-8
ERROR/AndroidRuntime(421): android.view.ViewRoot$CalledFromWrongThreadException:
Only the original thread that created a view hierarchy can touch its views.

究竟是怎么回事呢?原因在于,Android系统中的视图组件并非线程安全的,假设要更新视图,必须在主线程中更新。不能够在子线程中运行更新的操作。

既然这样,我们就在子线程中通知主线程,让主线程做更新操作吧。那么,我们怎样通知主线程呢?我们须要使用到Handler对象。

我们略微改动一下上面的代码:

public class MainActivity extends Activity implements View.OnClickListener {

	private static final int COMPLETED = 0;

	private TextView stateText;
private Button btn; private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.what == COMPLETED) {
stateText.setText("completed");
}
}
}; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
stateText = (TextView) findViewById(R.id.tv);
btn = (Button) findViewById(R.id.btn); btn.setOnClickListener(this);
} @Override
public void onClick(View v) {
new WorkThread().start();
} //工作线程
private class WorkThread extends Thread {
@Override
public void run() {
//......处理比較耗时的操作 //处理完毕后给handler发送消息
Message msg = new Message();
msg.what = COMPLETED;
handler.sendMessage(msg);
}
}
}

通过上面这样的方式,我们就能够解决线程安全的问题,把复杂的任务处理工作交给子线程去完毕,然后子线程通过handler对象告知主线程,由主线程更新视图。这个过程中消息机制起着关键的数据。

以下,我们就来分析一下Android中的消息机制。

熟悉Windows编程的朋友知道Windows程序是消息驱动的,而且有全局的消息循环系统。

Google參考了Windows的消息循环机制,也在Android系统中实现了消息循环机制。Android通过Looper、Handler来实现消息循环机制。Android的消息循环是针对线程的。每一个线程都能够有自己的消息队列和消息循环。

Android系统中的Looper负责管理线程的消息队列和消息循环。通过Looper.myLooper()得到当前线程的Looper对象,通过Looper.getMainLooper()得到当前进程的主线程的Looper对象。

前面提到。Android的消息队列和消息循环都是针对详细线程的,一个线程能够存在一个消息队列和消息循环,特定线程的消息仅仅能分发给本线程。不能跨线程和跨进程通讯。可是创建的工作线程默认是没有消息队列和消息循环的。假设想让工作线程具有消息队列和消息循环,就须要在线程中先调用Looper.prepare()来创建消息队列,然后调用Looper.loop()进入消息循环。以下是我们创建的工作线程:

	class WorkThread extends Thread {
public Handler mHandler; public void run() {
Looper.prepare(); mHandler = new Handler() {
public void handleMessage(Message msg) {
// 处理收到的消息
}
}; Looper.loop();
}
}

这样一来。我们创建的工作线程就具有了消息处理机制了。

那么。为什么前边的演示样例中,我们怎么没有看到Looper.prepare()和Looper.loop()的调用呢?原因在于,我们的Activity是一个UI线程。执行在主线程中,Android系统会在Activity启动时为其创建一个消息队列和消息循环。

前面提到最多的是消息队列(MessageQueue)和消息循环(Looper)。可是我们看到每一个消息处理的地方都有Handler的存在,它是做什么的呢?Handler的作用是把消息增加特定的Looper所管理的消息队列中,并分发和处理该消息队列中的消息。构造Handler的时候能够指定一个Looper对象,假设不指定则利用当前线程的Looper对象创建。以下是Handler的两个构造方法:

/**
* Default constructor associates this handler with the queue for the
* current thread.
*
* If there isn't one, this handler won't be able to receive messages.
*/
public Handler() {
if (FIND_POTENTIAL_LEAKS) {
final Class<? extends Handler> klass = getClass();
if ((klass.isAnonymousClass() || klass.isMemberClass() || klass.isLocalClass()) &&
(klass.getModifiers() & Modifier.STATIC) == 0) {
Log.w(TAG, "The following Handler class should be static or leaks might occur: " +
klass.getCanonicalName());
}
} mLooper = Looper.myLooper();
if (mLooper == null) {
throw new RuntimeException(
"Can't create handler inside thread that has not called Looper.prepare()");
}
mQueue = mLooper.mQueue;
mCallback = null;
} /**
* Use the provided queue instead of the default one.
*/
public Handler(Looper looper) {
mLooper = looper;
mQueue = looper.mQueue;
mCallback = null;
}

以下是消息机制中几个重要成员的关系图:

Android中的消息机制

Android中的消息机制

一个Activity中可以创建出多个工作线程,假设这些线程把他们消息放入Activity主线程的消息队列中,那么消息就会在主线程中处理了。

由于主线程一般负责视图组件的更新操作,对于不是线程安全的视图组件来说,这样的方式可以非常好的实现视图的更新。

那么,子线程怎样把消息放入主线程的消息队列中呢?仅仅要Handler对象以主线程的Looper创建,那么当调用Handler的sendMessage方法,系统就会把消息主线程的消息队列,而且将会在调用handleMessage方法时处理主线程消息队列中的消息。

对于子线程訪问主线程的Handler对象,你可能会问,多个子线程都訪问主线程的Handler对象,发送消息和处理消息的过程中会不会出现数据的不一致呢?答案是Handler对象不会出现故障,由于Handler对象管理的Looper对象是线程安全的,无论是加入消息到消息队列还是从消息队列中读取消息都是同步保护的,所以不会出现数据不一致现象。