欢迎来到天天文库
浏览记录
ID:40660105
大小:232.47 KB
页数:49页
时间:2019-08-05
《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
此文档下载收益归作者所有