2 Android消息机制2-Handler(Native层)

2 Android消息机制2-Handler(Native层)

ID:40660105

大小:232.47 KB

页数:49页

时间:2019-08-05

2 Android消息机制2-Handler(Native层)_第1页
2 Android消息机制2-Handler(Native层)_第2页
2 Android消息机制2-Handler(Native层)_第3页
2 Android消息机制2-Handler(Native层)_第4页
2 Android消息机制2-Handler(Native层)_第5页
资源描述:

《2 Android消息机制2-Handler(Native层)》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库

1、相关源码framework/base/core/java/andorid/os/MessageQueue.javaframework/base/core/jni/android_os_MessageQueue.hframework/base/core/jni/android_os_MessageQueue.cppsystem/core/include/utils/Looper.hsystem/core/libutils/Looper.cppsystem/core/libutils/RefBase.cppframework/native/include

2、/android/looper.hframework/base/native/android/looper.cpp一、概述在文章Android消息机制1-Handler(Java层)中讲解了Java层的消息处理机制,其中MessageQueue类里面涉及到多个native方法,除了MessageQueue的native方法,native层本身也有一套完整的消息机制,用于处理native的消息。在整个消息机制中,而MessageQueue是连接Java层和Native层的纽带,换言之,Java层可以向MessageQueue消息队列中添加消息,Nati

3、ve层也可以向MessageQueue消息队列中添加消息。Native层的关系图二、MessageQueue在MessageQueue中的native方法如下:privatenativestaticlongnativeInit();privatenativestaticvoidnativeDestroy(longptr);privatenativevoidnativePollOnce(longptr,inttimeoutMillis);privatenativestaticvoidnativeWake(longptr);privatenativest

4、aticbooleannativeIsPolling(longptr);privatenativestaticvoidnativeSetFileDescriptorEvents(longptr,intfd,intevents);2.1nativeInit()初始化过程的调用链如下:下面来进一步来看看调用链的过程:【1】newMessageQueue()==>MessageQueue.javaMessageQueue(booleanquitAllowed){mQuitAllowed=quitAllowed;mPtr=nativeInit();//mPt

5、r记录native消息队列的信息【2】}【2】android_os_MessageQueue_nativeInit()==>android_os_MessageQueue.cppstaticjlongandroid_os_MessageQueue_nativeInit(JNIEnv*env,jclassclazz){NativeMessageQueue*nativeMessageQueue=newNativeMessageQueue();//初始化native消息队列【3】if(!nativeMessageQueue){jniThrowRuntime

6、Exception(env,"Unabletoallocatenativequeue");return0;}nativeMessageQueue->incStrong(env);returnreinterpret_cast(nativeMessageQueue);}【3】newNativeMessageQueue()==>android_os_MessageQueue.cppNativeMessageQueue::NativeMessageQueue():mPollEnv(NULL),mPollObj(NULL),mExceptionO

7、bj(NULL){mLooper=Looper::getForThread();//获取TLS中的Looper对象if(mLooper==NULL){mLooper=newLooper(false);//创建native层的Looper【4】Looper::setForThread(mLooper);//保存native层的Looper到TLS中}}·Looper::getForThread(),功能类比于Java层的Looper.myLooper();·Looper::setForThread(mLooper),功能类比于Java层的ThreadL

8、ocal.set();MessageQueue是在Java层与Native层有着紧密的联系,但是此次Nati

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。