浅析Android中的消息机制-解决:Only the original thread that created a view hierarchy can touch its views.
来源:互联网 发布:摄像头电脑监控器软件 编辑:程序博客网 时间:2024/04/30 06:40
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); }@Overridepublic void onClick(View v) {new WorkThread().start();}//工作线程private class WorkThread extends Thread {@Overridepublic void run() {//......处理比较耗时的操作//处理完成后改变状态stateText.setText("completed");}}}
这段代码似乎看上去很正常,但是当你运行时就会发现,它会报一个致命性的异常:
ERROR/AndroidRuntime(421): FATAL EXCEPTION: Thread-8ERROR/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() {@Overridepublic 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); }@Overridepublic void onClick(View v) {new WorkThread().start();}//工作线程private class WorkThread extends Thread {@Overridepublic 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; }
下面是消息机制中几个重要成员的关系图:
一个Activity中可以创建出多个工作线程,如果这些线程把他们消息放入Activity主线程的消息队列中,那么消息就会在主线程中处理了。因为主线程一般负责视图组件的更新操作,对于不是线程安全的视图组件来说,这种方式能够很好的实现视图的更新。
那么,子线程如何把消息放入主线程的消息队列中呢?只要Handler对象以主线程的Looper创建,那么当调用Handler的sendMessage方法,系统就会把消息主线程的消息队列,并且将会在调用handleMessage方法时处理主线程消息队列中的消息。
对于子线程访问主线程的Handler对象,你可能会问,多个子线程都访问主线程的Handler对象,发送消息和处理消息的过程中会不会出现数据的不一致呢?答案是Handler对象不会出现问题,因为Handler对象管理的Looper对象是线程安全的,不管是添加消息到消息队列还是从消息队列中读取消息都是同步保护的,所以不会出现数据不一致现象。
深入理解Android消息处理机制对于应用程序开发非常重要,也可以让我们对线程同步有更加深刻的认识,希望这篇文章可以对朋友们有所帮助。
文章转载源处:http://blog.csdn.net/liuhe688/article/details/6407225#
- 浅析Android中的消息机制-解决:Only the original thread that created a view hierarchy can touch its views.
- 浅析Android中的消息机制-解决:Only the original thread that created a view hierarchy can touch its views.
- 浅析Android中的消息机制-解决:Only the original thread that created a view hierarchy can touch its views.
- 浅析Android中的消息机制-解决:Only the original thread that created a view hierarchy can touch its views.
- 浅析Android中的消息机制-解决:Only the original thread that created a view hierarchy can touch its views. .
- 浅析Android中的消息机制-解决:Only the original thread that created a view hierarchy can touch its views.
- Android 解决Only the original thread that created a view hierarchy can touch its views异常
- 报Only the original thread that created a view hierarchy can touch its views错误解决
- 报Only the original thread that created a view hierarchy can touch its views错误解决
- 报Only the original thread that created a view hierarchy can touch its views错误解决
- 解决Only the original thread that created a view hierarchy can touch its views
- 解决:Only the original thread that created a view hierarchy can touch its views
- Only the original thread that created a view hierarchy can touch its views的解决记录
- 完美解决Only the original thread that created a view hierarchy can touch its views
- 已解决:Only the original thread that created a view hierarchy can touch its views.
- Android Only the original thread that created a view hierarchy can touch its views.
- Android单元测试 Only the original thread that created a view hierarchy can touch its views.
- android:Only the original thread that created a view hierarchy can touch its views
- Centos上eucalyptus源代码安装-安装euca2ools
- C++ 基础之拷贝构造函数
- SQLServer之深度分析Insert
- linux 内核代码树介绍
- java 调用命令行 备份mysql数据库
- 浅析Android中的消息机制-解决:Only the original thread that created a view hierarchy can touch its views.
- How do I configure glibc in a separate build folder ?与 Glibc 安装指南
- MFC类层次图
- strust2如何访问servlet api
- 用C++实现设计模式中的策略模式
- Spring说明文档翻译2——第二部分 Spring3.0新特性
- 什么是堆栈
- SQLServer之深度分析Select
- 移居 B3log