In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >
Share
Shulou(Shulou.com)06/01 Report--
What is Context?
Context literally means "context", so what exactly does this "context" mean? what does "up" mean? what does "down" specify?
Personal understanding, contribute to their own understanding of the way, not necessarily right, if there are better views can be communicated and studied together. As far as I understand it, "above" means that when APP starts, it has something to do with Context. At this time, it is still in the startup stage of the system, so it has a connection with Context.
"under" specifies that after the APP interface is displayed normally, during the normal interaction with the user, you can use Context to obtain resources, system services, and so on. So Context's so-called context is the connecting link between the preceding and the following.
The inheritance relationship of Context's class
As you can see from the above figure, there is only one implementation class in Context, ContextImpl,Application,Service,Activity, which directly or indirectly inherits a variable mBase in ContextWrapper,ContextWrapper, which is also a Context type, which is actually an object of ContextImpl, and all operations of Application,Service,Activity are delegated to the mBase implementation.
Since Context is a connecting link and is a context related to the APP startup process, let's first take a look at the system startup process and the APP startup process, both in the same diagram, as follows:
As can be seen from the above picture:
First of all, the first process of the system, init, runs and does some initialization-related work, and then a zygote process is sent out through the system call fork, and the init process is blocked there by poll.
After the zygote process is fork out by the init process, do some jni initialization, etc., a SystemServer process is created by calling fork. All the Android system services run in the SystemServer process. Zygote calls the java code through jni, fork leaves the SystemServer process, opens the door to the java world, and blocks itself through sokcet snooping, listening for messages from socket.
After the SystemServer process is fork, many system services will be opened, such as AMS,PMS,WMS, etc. The SystemServer process is a very important process, many important services of the system reside in this process, and the SystemServer process is blocked there through the Handler mechanism. Wait for the message and process the message. So the whole system starts up.
In PMS, Lanuher APP is launched and users can see the Android desktop. If the user clicks the QQ icon on the desktop, the Lannuher process communicates with AMS through the binder mechanism, telling AMS to start the QQ application. After AMS receives the message from the Lanuher process, it sends a message to the zygote process through socket. We know that the zygote process has been listening to the socket since the SystemServer process left the fork, so after the zygote process receives the message from the SystemServer process, if the QQ process has not been created, at this time Zygote will call the fork system call to create a new process to run the QQ code, which is the QQ process. The first call after the QQ App process is created is the Activity.main () method, as shown in the figure above.
What is the purpose of Context?
1 access the resources of the current application
GetResources
GetAssets
2 start other components
Activity
Service
3 access to system services
GetSystemService
The ContextImpl object creation process of Application object
1 through the above analysis of the system startup process and APP startup process, we can see that the first APP application is started by AMS creating a new process through the binder mechanism, and then calling the main method in the ActivityThread class. Many people may wonder why Android is also implemented on Java, and why don't you see the main method? In fact, the entrance to the whole App application is the ActivityThread.main method. All the creation of Application and Activity,Service is in the ActivityThread class, which is actually the main thread of our App. In ActivityThread, there is a method, performLaunchActivity (), in which Application,Activity is created. Let's take a look at this method.
The figure below is as follows
Call the mInstrumentation.newActivity () method to create the Activity. The newActivity code is shown below.
As you can see, after the Activity is created, the attach method of Activity is called, and the first parameter is context, where Activity has contact with context, and this context is actually an instance of ContextImpl. Then the first picture above, performLaunchActivity, continues to go down, as shown below:
As you can see from the figure above, after the activity is created and the activity.attach () method is called, the Application object is created, as follows
Application app = r.packageInfo.makeApplication (false,mInstrumentation)
The makeApplication code is as follows:
Then an instance of ContextImpl is created, with the following code
/ / create ContextImpl instance ContextImpl appContext = new ContextImpl (); / / initialize ContextImpl instance appContext.init (this,null,mActivityThread); / / pass appContext to newApplication method app = mActivityThread.mInstrumentation.newApplication (cl,appClass,appContext); / / contact with Application. ContextImpl instance saves Application instance appappContext.setOuterContext (app); mActivityThread.mAllApplication.add (app); mApplication = app
Let's take a look at the newApplicaiton () method, which has the following code:
Static public Application newApplication (Class clazz,Context context) {/ / create Application instance Application app = (Application) clazz.newInstance () through reflection; / / save the ContextImpl instance passed above to app.attach (context) through attach method; / / return Application instance return app;}
After creating the Application and contacting the application instance by calling the attach method of the ContextImpl instance, it's time to call the onCreate () method of application. The code is as follows:
Public void callApplicationOnCreate (Application app) {/ / call Application's onCreate method app.onCreate ();}
After creating the activity, creating the application, and calling the attach and onCreate methods of application, it's time to call the onCreate method of activity. Still in the performLanucherActivity method. As shown below:
As shown in the figure above, onCreate () of activity is called through the callActivityOnCreate () method of mInstructation. This code is similar to the logic of calling the onCreate () method of application, which you can see for yourself. The code is no longer posted here
* from the above analysis, we can know when Context was created, how it was created, when it was initialized, and how it contacted Application and Activity. *
Why is the life cycle of Application so long?
We know that the life cycle of Application is as long as that of APP processes, and so is the context corresponding to Application. Why is the life cycle of Application so long?
As you can see from the above analysis, the entry to the APP process is the ActivityThread.main () method, in which an instance of ActivityThread is created and blocked there by Handler. The instance of ActivityThread will always exist, if APP does not exit, and the instance of ActivityThread stores the mAllApplications of application, which is an array that saves the Application instance. When creating the Application instance, the app is saved through mActivityThread.mAllApplications.add (app). You can know by strong reference that ActivityThread references the application instance, while the ActivityThread instance is in the process cycle, blocking all the time, so the life cycle of Application is the same as the process.
Pay attention to the usage of Context
Here is a picture to illustrate the differences and usage of several Context.
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.