Context in android----the context you don't know

Source: Internet
Author: User
<span id="Label3"></p><p><p>Transferred from: http://blog.csdn.net/qinjuning/article/details/7310620</p></p><p><p>Hello everyone, today to introduce us in the application development of the most familiar and unfamiliar Friends-----context class, said it is familiar with, it should be for us in the development of the moment in dealing with it, for example: Service, broadcastreceiver, Activity and so will use the relevant methods of the context, said it is unfamiliar, because we really do not understand the principle of the context, class structure relationship. A simple question is, How many context instance objects exist in an app app? one, two? Let's sell a Xiaoguanzi here. Read the article, I believe you will be enlightened.</p></p><p><p><span style="line-height: 1.5;">In the context, the Chinese literal is "contextual", which is described in the SDK as Follows:</span></p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">Interface to global information on an application Environment. This was an abstract class whose implementation was provided by the Android System. It allows access to application-specific resources and classes, as-well-up-calls for application-level operations such As launching activities, broadcasting and receiving intents, etc</p></p><p><p><span style="line-height: 1.5;">From the last three points, namely:</span></p></p><p><p>1, It describes the information of an application environment, that is, the Context.</p></p><p><p>2. The class is an abstract class, and Android provides a concrete implementation class for that abstract class (we'll talk about the Contextiml class later).</p></p><p><p>3, through it we can get the resources and classes of the application, also include some application-level operations, such as: start an activity, send broadcasts, accept Intent information, etc...</p></p><p><p><span style="line-height: 1.5;">thus, We can use this context object to build an Application-level operation (application-level operations).</span></p></p>I. The inheritance relationship of context related classes<p><p></p></p><p><p><strong>Related class Description:</strong></p></p><p><p><strong style="line-height: 1.5;">Context Class</strong> <span style="line-height: 1.5;">Path:/frameworks/base/core/java/android/content/context.java</span></p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">Description: an abstract class that provides a common set of Apis.</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">The source code (part) is as Follows:</p></p><pre><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Abstract</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span><span style="color: #000000;"><span style="color: #000000;">Context {...</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Abstract</span></span>Object Getsystemservice (String name);<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Get System-level Services</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Abstract</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>StartActivity (Intent Intent);<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">start activity with a intent</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Abstract</span></span>ComponentName StartService (Intent service);<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Start Service</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">get Sharedpreferences object based on file name</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Abstract</span></span>Sharedpreferences getsharedpreferences (String name,<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">mode); ...}</span></span></pre><p><p><strong>Contextiml.java class</strong> Path:/frameworks/base/core/java/android/app/contextimpl.java</p></p><p style="margin-left: 120px;"><p style="margin-left: 120px;">Description: the implementation class of the context class is contextiml, which implements the function of the context class. Note that most of the functionality of the function is to call its properties mpackageinfo to complete, as we'll see Later.</p></p><p style="margin-left: 120px;"><p style="margin-left: 120px;">The source code (part) is as Follows:</p></p><pre><span style="color: #008000;"><span style="color: #008000;">/**</span></span><span style="color: #008000;"><span style="color: #008000;">* Common Implementation of the context API, which provides the base * Context object for Activity and other application C Omponents. </span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span>Contextimpl<span style="color: #0000ff;"><span style="color: #0000ff;">extends</span></span><span style="color: #000000;"><span style="color: #000000;">context{</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">all application programs common one Mpackageinfo object</span></span> <span style="color: #008000;"><span style="color: #008000;">/*</span></span><span style="color: #008000;"><span style="color: #008000;"></span> package</span><span style="color: #008000;"><span style="color: #008000;">*/</span></span><span style="color: #000000;"><span style="color: #000000;">Activitythread.packageinfo mpackageinfo; @Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span><span style="color: #000000;"><span style="color: #000000;">Object getsystemservice (String name) {...</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Else</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span><span style="color: #000000;"><span style="color: #000000;">(activity_service.equals (name)) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">Getactivitymanager (); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Else</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span><span style="color: #000000;"><span style="color: #000000;">(input_method_service.equals (name)) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span>Inputmethodmanager.getinstance (<span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span><span style="color: #000000;"><span style="color: #000000;">); }} @Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">startactivity (Intent Intent) {...</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Start an activity</span></span><span style="color: #000000;"><span style="color: #000000;">mmainthread.getinstrumentation (). execstartactivity (getoutercontext (), mmainthread.getapplicationth Read (),</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span>,<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span>, intent,-1<span style="color: #000000;"><span style="color: #000000;">); }}</span></span></pre><p><p><strong>Contextwrapper class</strong> Path: \frameworks\base\core\java\android\content\contextwrapper.java</p></p><p style="margin-left: 90px;"><p style="margin-left: 90px;">Description: like its name, This class is just a wrapper for the context class whose constructor contains a true context reference, the Contextim object.</p></p><p style="margin-left: 150px;"><p style="margin-left: 150px;">The source code (part) is as Follows:</p></p><pre><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span>Contextwrapper<span style="color: #0000ff;"><span style="color: #0000ff;">extends</span></span><span style="color: #000000;"><span style="color: #000000;">Context {context mbase; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">This property points to an CONTEXTIML instance, which is typically assigned when creating application, Service, activity</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Create application, Service, Activity, Call this method to assign a value to the Mbase property</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">protected</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">Attachbasecontext (Context Base) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(mbase! =<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Throw</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>IllegalStateException ("Base Context already set"<span style="color: #000000;"><span style="color: #000000;">); } mbase</span></span>=<span style="color: #000000;"><span style="color: #000000;">base; } @Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">startactivity (Intent Intent) {mbase.startactivity (Intent); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">calling the Mbase instance method</span></span><span style="color: #000000;"><span style="color: #000000;"> }}</span></span></pre><p><p><strong>Contextthemewrapper class</strong> Path:/frameworks/base/core/java/android/view/contextthemewrapper.java</p></p><p style="margin-left: 180px;"><p style="margin-left: 180px;">Description: the class internally contains a topic (Theme)-related interface, which is specified by the Android:theme Property. Only the activity requires a theme, and the service does not need a theme, so the service inherits directly from the Contextwrapper class.</p></p><p style="margin-left: 180px;"><p style="margin-left: 180px;">The source code (part) is as Follows:</p></p><pre><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span>Contextthemewrapper<span style="color: #0000ff;"><span style="color: #0000ff;">extends</span></span><span style="color: #000000;"><span style="color: #000000;">Contextwrapper {</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">This property points to an CONTEXTIML instance, which is typically assigned when creating application, Service, activity</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span><span style="color: #000000;"><span style="color: #000000;">Context mbase; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Mbase The same way there are two ways to assign values</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span>Contextthemewrapper (Context base,<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">themeres) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Super</span></span><span style="color: #000000;"><span style="color: #000000;">(base); Mbase</span></span>=<span style="color: #000000;"><span style="color: #000000;">base; Mthemeresource</span></span>=<span style="color: #000000;"><span style="color: #000000;">themeres; } @Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">protected</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">Attachbasecontext (Context Newbase) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Super</span></span><span style="color: #000000;"><span style="color: #000000;">. Attachbasecontext (newbase); Mbase</span></span>=<span style="color: #000000;"><span style="color: #000000;">newbase; }}</span></span></pre><p><p>Activity class, Service class, Application class are essentially context subclasses, more information you can refer to the source code to Understand.</p></p>Ii. when to create a context instance<p><p>Having become familiar with the Context's inheritance, we then analyze what situations the application needs to create a context object for? Application to create a context instance of the</p></p><p><p>There are several situations where:</p></p><p><p><strong>1. When creating application objects, and the entire app has a application object</strong></p></p><p><p><strong>2. When creating service objects</strong></p></p><p><p><strong>3. When you create an Activity object</strong></p></p><p><p><span style="line-height: 1.5;">So the number of context formulas common to the app app Is:</span></p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;"><strong style="line-height: 1.5;">Total number of context instances = Number of Service + activity + 1 (application corresponding context Instance)</strong></p></p><p><p><strong style="line-height: 1.5;">The opportunity to create a context specifically</strong></p></p><p><p><span style="line-height: 1.5;">1. Time to create application objects</span></p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;"><span style="line-height: 1.5;">the first time each application starts, the Application object is created First. If it is clear that an activity (startactivity) process is started for an application</span> , the time to create the application is in the Create Handlebindapplication () method, which is located in the The Activitythread.java class, as Follows:</p></p><pre><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">CONTEXTIML instances created at the same time when creating application</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Final</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">handlebindapplication (appbinddata data) {...</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">/create Application Object</span></span>Application app = Data.info.makeApplication (data.restrictedbackupmode,<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">); ...}</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span>Application Makeapplication (<span style="color: #0000ff;"><span style="color: #0000ff;">Boolean</span></span><span style="color: #000000;"><span style="color: #000000;">forcedefaultappclass, Instrumentation Instrumentation) { ... </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Try</span></span><span style="color: #000000;"><span style="color: #000000;">{java.lang.ClassLoader CL</span></span>=<span style="color: #000000;"><span style="color: #000000;">getClassLoader (); Contextimpl AppContext</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>Contextimpl ();<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Create a Contextimpl object instance</span></span>Appcontext.init (<span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span>,<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span>, mactivitythread);<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">initialize the related properties of the CONTEXTIML instance</span></span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">/create A new Application object</span></span>App =<span style="color: #000000;"><span style="color: #000000;">mActivityThread.mInstrumentation.newApplication (cl, appclass, appContext); Appcontext.setoutercontext (app); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Pass the application instance to the Contextimpl instance</span></span><span style="color: #000000;"><span style="color: #000000;"> } ...}</span></span></pre><p align="left"><p align="left">2. Time to create Activity objects</p></p><p style="margin-left: 30px;" align="left"><p style="margin-left: 30px;" align="left"><span style="line-height: 1.5;">When an activity is started by startactivity () or Startactivityforresult () request, if the system detects that a new activity object needs to be created, it</span> Callback Handlelaunchactivity () method, which then calls the Performlaunchactivity () method to create an activity instance, and callback OnCreate (), OnStart () method, etc., The function is located in the The Activitythread.java class, as Follows:</p></p><pre><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">create <span style="color: #008000;">a CONTEXTIML instance while creating an activity instance</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Final</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">handlelaunchactivity (activityrecord r, Intent customintent) {... Activity a</span></span>= Performlaunchactivity (r, customintent);<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Start an activity</span></span><span style="color: #000000;"><span style="color: #000000;">}</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Final</span></span><span style="color: #000000;"><span style="color: #000000;">Activity performlaunchactivity (activityrecord r, Intent customintent) {... Activity Activity</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Try</span></span><span style="color: #000000;"><span style="color: #000000;"> { </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Create an Activity object instance</span></span>Java.lang.ClassLoader cl =<span style="color: #000000;"><span style="color: #000000;">R.packageinfo.getclassloader (); Activity</span></span>=<span style="color: #000000;"><span style="color: #000000;">minstrumentation.newactivity (cl, component.getclassname (), r.intent); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(activity! =<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">) {contextimpl AppContext</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>Contextimpl ();<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Create an Activity instance</span></span>Appcontext.init (r.packageinfo, r.token,<span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span>);<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">initialize the related properties of the CONTEXTIML instance</span></span>Appcontext.setoutercontext (activity);<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Pass the activity information to the Contextimpl instance</span></span><span style="color: #000000;"><span style="color: #000000;"> ... } ... }</span></span></pre><p align="left"><p align="left">3. Time to create service objects</p></p><p style="margin-left: 30px;" align="left"><p style="margin-left: 30px;" align="left"><span style="line-height: 1.5;">with StartService or bindservice, if the system detects that a new service instance needs to be created, it will callback the Handlecreateservice () method and</span> complete the relevant data Operation. The Handlecreateservice () function is located in the Activitythread.java class, as Follows:</p></p><pre><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">create <span style="color: #008000;">a CONTEXTIML instance while creating a service instance</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Final</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">handlecreateservice (createservicedata data) {...</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Create a service instance</span></span>Service service =<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Try</span></span><span style="color: #000000;"><span style="color: #000000;">{java.lang.ClassLoader CL</span></span>=<span style="color: #000000;"><span style="color: #000000;">Packageinfo.getclassloader (); Service</span></span>=<span style="color: #000000;"><span style="color: #000000;">(Service) cl.loadclass (data.info.name). newinstance (); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Catch</span></span><span style="color: #000000;"><span style="color: #000000;">(Exception e) {} ... Contextimpl Context</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>Contextimpl ();<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Create a Contextimpl object instance</span></span>Context.init (packageinfo,<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span>,<span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span>);<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">initialize the related properties of the CONTEXTIML instance</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">get the Application object information we created earlier</span></span>Application app = Packageinfo.makeapplication (<span style="color: #0000ff;"><span style="color: #0000ff;">false</span></span><span style="color: #000000;"><span style="color: #000000;">, minstrumentation); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Pass the service information to the Contextimpl instance</span></span><span style="color: #000000;"><span style="color: #000000;">Context.setoutercontext (service); ...}</span></span></pre><p align="left"><p align="left">In addition, it is important to emphasize that, through the analysis of contextimp, most of the operations of its methods are directly calling its property Mpackageinfo (the attribute Class</p></p><p align="left"><p align="left">Type packageinfo). This shows that <strong>Contextimp is a lightweight class, and PackageInfo is the real heavyweight class</strong> . And the one in the app</p></p><p align="left"><p align="left">All CONTEXTIML instances correspond to the same packageinfo Object.</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;"></p></p><p><p>Context in android----the context you don't know</p></p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.