In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to realize the constructor of Android". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to realize the constructor of Android".
What the function prepareMainLooper does is actually create a Looper object in the thread, which is stored in the sThreadLocal member variable.
The member variable sThreadLocal is of type ThreadLocal, indicating that it is a thread-local variable, which ensures that there is a separate Looper object in each thread that calls the prepareMainLooper function.
When the thread creates the Looper object, it is done by the prepare function, and when the Looper object is created, a message queue MessageQueue is created, which is stored in the member variable mQueue of Looper, and the subsequent messages are stored in this queue.
Message queuing is the most important component in the message handling mechanism of Android applications, so let's take a look at its creation process, that is, the implementation of its constructor.
Implement the following in frameworks/base/core/java/android/os/MessageQueue.java file:
[java] view plaincopypublic class MessageQueue {. Private int mPtr; / / used by native code private native void nativeInit (); MessageQueue () {nativeInit ();}. }
Its initialization is done by the JNI method nativeInit, which is defined in the frameworks/base/core/jni/android_os_MessageQueue.cpp file:
[cpp] view plaincopystatic void android_os_MessageQueue_nativeInit (JNIEnv* env, jobject obj) {NativeMessageQueue* nativeMessageQueue = new NativeMessageQueue (); if (! NativeMessageQueue) {jniThrowRuntimeException (env, "Unable to allocate native queue"); return;} android_os_MessageQueue_setNativeMessageQueue (env, obj, nativeMessageQueue);}
In JNI, a message queuing NativeMessageQueue,NativeMessageQueue class is created accordingly, which is also defined in the frameworks/base/core/jni/android_os_MessageQueue.cpp file, and its creation process is as follows:
[cpp] view plaincopyNativeMessageQueue::NativeMessageQueue () {mLooper = Looper::getForThread (); if (mLooper = = NULL) {mLooper = new Looper (false); Looper::setForThread (mLooper);}}
It mainly creates a Looper object internally. Note that this Looper object is implemented in the JNI layer. It is different from the Looper in the upper Java layer, but they correspond. When we further analyze the process of the message loop below, the reader will clearly understand the relationship between them.
The process of creating this Looper is also important, but for a moment, let's analyze the holding line of the android_os_MessageQueue_nativeInit function, which creates the local message queue NativeMessageQueue object, and then calls the android_os_MessageQueue_setNativeMessageQueue function to save the message queue object in the mPtr member variable of the MessageQueue object we created in the Java layer earlier:
[cpp] view plaincopystatic void android_os_MessageQueue_setNativeMessageQueue (JNIEnv* env, jobject messageQueueObj, NativeMessageQueue* nativeMessageQueue) {env- > SetIntField (messageQueueObj, gMessageQueueClassInfo.mPtr, reinterpret_cast (nativeMessageQueue);}
The parameter messageQueueObj passed here is the message queue object we created earlier in the Java layer, and gMessageQueueClassInfo.mPtr represents the offset of its member variable mPtr in the Java class MessageQueue. Through this offset, the local message queue object natvieMessageQueue can be saved in the mPtr member variable of the message queue object created by the Java layer, so that when we call other member functions of the message queue object of the Java layer to enter the JNI layer. The message queue object corresponding to it in the JNI layer can be easily found.
Thank you for reading, the above is the content of "how to realize the constructor of Android". After the study of this article, I believe you have a deeper understanding of how to realize the constructor of Android, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.