The relationship between contexts

ContextIml, ContextWrapper, ContextIml, ContextIml, ContextWrapper, ContextIml, ContextIml, ContextWrapper, ContextIml, ContextWrapper, ContextIml, ContextWrapper, ContextIml

Context. GetResource is called as follows:

This part mainly involves the reading of the source code. Here is a UML sequence diagram for those interested to read by themselves:

Usage of Context:

  1. Get screen orientation int type, 1: portrait, 2: landscape
context.getResources().getConfiguration().orientation;
Copy the code
  1. The resource file name is index. HTML, and the suffix.htm is not required
InputStream fs =this.getResources().openRawResource(R.raw.index.htm); 
Copy the code
    InputStreamReader read = new InputStreamReader (fs,"utf-8");    
    BufferedReader in = new BufferedReader(read);
Copy the code
  1. Read the PNG or BMP file in res/drawable
  • Get the Resources object
Resources r = this.getContext().getResources();    
Copy the code
  • Read a resource as a data stream
Inputstream is = r.openRawResource(R.drawable.mm_image);    
BitmapDrawable bmpDraw = new BitmapDrawable(is);    
Bitmap bmp = bmpDraw.getBitmap();
Copy the code
  1. Get screen density
float density = getResources().getDisplayMetrics().density;
Copy the code
  1. Gets the number of pixels defined by the Dimens resource
 Context.getResources().getDimensionPixelSize();
Copy the code
  1. Get color resources
 Context.getResources().getColor(R.color.colorId);
Copy the code
  1. Getting string resources
Context.getResources().getString(R.string.stringId);
Copy the code
  1. Get an array of strings:
String[] roles = context.getResources().getStringArray(R.array.array_role_values);  
Copy the code

Complete Resource utilization process (from application startup)

  1. Here, in the home page click App, is running in the process of the Launcher, through ActivityManagerServiceBinder IPC for startActivity system_server process the request
  2. After receiving the request, the system_server Process sends a request to the Zygote Process to create the Process through the process. start method

3. Enter activityThread. main, which runs in the App process. Through ActivityManagerServiceBinder IPC to initiate attachApplication system_server process request 4. Server send Binder thread Secheduled the launcher, Binder sends LaunchActivity 5.activity. Thread to execute

  • Create contex: ContextImpl context = ContextImpl. CreateAppContext (this, packageInfo);
  • Resources = PackageInfo.getResources (mainThread); (LoadedApk type)
  • CreateResourcesImpl calls createAssetManager and binds addAssetPath

We will do the same with Resource retrieval as described above, without further elaboration.

Q&A

How many contexts in an application? Number of activities + Number of Services + Number of Applications (usually 1, only one Application in a process)

Expansion and extension

  1. Java class loading mechanism

2.H5 loading process, React Native, Weex