Create the application window in Android

Recommended for you: Get network issues from WhatsUp Gold. Not end users.
The article such as the front of the application window, each corresponding to a Activity, so to create the application window, first create the Activity.
The relationship between Context and Activity in the post also explain the Activity creation process, I will directly to the source, direct plus some
The necessary notes, if you do not understand, can view the source code, not every line of code one one explained.


ps: Technical ability is limited, there will be wrong, please correct me, thank you...


General steps into the next:
  1. Create Activity
  2. Create Window
  3. Add View to the window
  4. Tells WindowManagerService to load onto the screen window



Create Activity


ActivityManagerService to the ApplicationThread scheduleLaunchActivity through the proxy remote call ApplicationThread () method,
The ApplicationThread class is a ActivityThread inner class.
The code into the next:



The method to do two things: a thing that created the data object ActivityClientRecorder to a local Activity; a matter through asynchronous message send H a startup Activity to the main thread's message queue.
Note that here at r.token, the token is a Binder object (ActitityRecorder), is ActivityManagerService remote transfer over, use this token enables interprocess communication and remote ActivityManagerService.


Through H (Handler) and call out the method of ActivityThread: handleLaunchActivity()



Then call performLaunchActivity () method



.......
.......



Through the ClassLoader to create a Activity instance
Continue this method:



See Activity attach () method



Part parameters explain, token ActivityServiceManager is passed to the remote ActivityRecorder, parent is the need to start the Activity parent Activity, this is ActivityGroup to start Activity, this is not the case for null, the other is null.


Start creating window.


Below is the attach () method specific code







Examples of began to create a Window from here: 3756 lines
mWindow = PolicyManager.makeNewWindow(this);
mWindow.setCallback(this);

The first sentence is to create a window object, the second is setting a callback interface for Activity, Activity implements the Window.Callback interface


Look at this PolicyManager,
The PolicyManager is in this package com.android.internal.policy, see the PolicyManager source code that can know, the real operation
He did not, but the com.android.internal.policy.impl.Policy, is the realization of the com.android.internal.policy.IPolicy interface,
Activity PolicyManager is just a wrapper for a layer of com.android.internal.policy.impl.Policy.
The following is to create an instance of Ipolicy source code, directly in the load when the byte code has already been instantiated.



A total of three methods of com.android.internal.policy.IPolicy:

SeePolicyManager.makeNewWindow(this);This method:

Call the com.android.internal.policy.impl.Policy makeNewWindow()


Down down

Here new a PhoneWindow to return. Parameter context is passed over the Activity
Which method of constructing PhoneWindow assigns context to value the mContext, create a current Activity LayoutInflater
The code into the next:

Call the superclass constructor:

So the application window (Window.getContext) is to return the Activity instance


Here Window has been established, we return to Activity (attach) method:
Examples of mWindow is PhoneWindow.



The 3777 line object is Window FuvalueWindowManager
Go see:



Window appToken is Activity mToken.
Call this method when WM is null, so when the first call instance will create a WindowManagerImpl, is the single case, the same
Only one instance of WindowManagerImpl. And then to package the WM into the LocalWindowManager, we can see that the LocalWindowManager
Just a shell. Then the shell back to Activity.
Go back to Activity attach () method:
3781 for the instance of WindowManager Fu value to Activity mWindowManager.


Go back to ActivityThread performLaunchActivity () method

The 1611 line is called the Activity onCreate () method
The code in Instrumentation is as follows:

The 1047 line is called the activity onCreate () method
In the onCreate Activity method of overloading is our application programmer thing, we usually have to call the setContentView method in the conCreate () ().
The layout of the document or View object sets in, after the interface appeared.


Start adding View.


Have a look below this process:
In the Activity onCreate () method

MCalled said the onCreate method is called.


Our own Activity code is written so:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate();
setContentView(R.layout.main);

}


This method we override the parent class, the application code is not called by himself but by the Android system call, then this is the so-called callback mode, usually
Platform level products are designed so that the design, is to give developers a standard template, equivalent to the protocol, a program platform, generally in accordance with the platform
This kind of mode to write, advantage is: for the platform, controllable. For developers, simple.


Application developers and call the Activity setContentView()Method, there are several overloaded, look at only one, let's go in and have a look how the implementation of this method:



GetWindow is the PhoneWindow object we created above.
Enter PhoneWindow setContentView () method

MContentParent is a ViewGroup, if empty words, will be executed
installDecor();
Look at this method:

The first time to load when mDecor is null, the generateDecor () method will create a DecorView object Fu value to mDecor, by new DecorView(getContext(), -1),
DecorView is a ViewGroup, is a subclass of FramLayout, the DecorView is the entire window, including the title bar, we see the root View





generateLayout(mDecor)Method to find the mContentParent
This place is to display the title bar or not to do some operation, so when we do not require the system to provide the title bar in setContentView (why) before
Tell the system to remove the title bar, in the setContentView () after telling system remove causes error.
Back to PhoneWindow setContentView () method
The 207 line, mLayoutInflater.inflate (layoutResID, mContentParent) this method the layout file application developers came to loading
MContentParent.
MContentParent is a subset of mDecor view, in the title bar below, mContentParent is used to store the application developer view.
Please see the view hierarchy:






The 208 line to the end, the callback Activity onContentChanged () method, told the activity the contents of the view change.


Here, go back to the ActivityThread performLaunchActivity()



In here we can see that the concreate method is not called Activity, the system is not allowed.

The set mActivities is Activity data object, used to represent all Activity



Go back to ActivityThread handleLaunchActivity () method:









handlerResumeActivity()Is to perform Actitiviy onResume (to) a series of judging method before, and from onCreate to onResume () () statement cycle function.


To add to the window display




Then look at the





willBeVisible = ActivityManagerNative.getDefault().willActivityBeVisible(
a.getActivityToken());

Remote procedure call (ActivityManagerService willActivityBeVisible) method, the Activity inside the mToken passed, mToken is in fact a remote ActivityRecorder Binder reference, the ActivityRecorder represents the Activity object server. ActivityRecorder implements IBinder.
The above method is mainly to check the existence of the upcoming Activity distal.



Continue to go

The above willBeVisible is true,
Execute here.
Call the Activity method (makeVisible)



Then call the WindowManager method (addView),
Call the Window.LocalWindowManager method addView (), do some tests, like the following code is to check whether the sub window, then do some data
Handle.
''
Then call the WindowManagerImpl method (addView)




The following addView method overloading:

First create a ViewRoot object,
Create three arrays

MViews is stored in the DecorView
MRoots and DecorView is the corresponding ViewRoot
MParams is the corresponding WindowManager.LayoutParams
Each index corresponds to the same window.

Then call the ViewRoot method of setView

First issue a repaint request, in the draw after the window is displayed on the screen.


Then through the sWindowSession notifies the WindowManagerService window can be added to the screen.
This application window is created successfully. In fact, one of the most important operation is the code above, the former was made a preparation for here.




Explain the sWindowSesion object

The object is to obtain ViewRoot a static method, that is to say the entire application only one IWindowSession object




IWindowMananger Binder referenced by the ServiceManager object, then get the remote can and WindowManagerService Communications
The remote Session object reference object.
A Session mSession object in the WindowManagerService, this object is used, can communicate through mSession and WindowManagerService.




Here is the relationship between the various classes, ActivityThread did not put in, you know you can.








Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Ed at December 03, 2013 - 5:59 AM