Android中Context详解 ---- 你所不知道的Context(一)

来源:互联网 发布:php socket epoll 编辑:程序博客网 时间:2024/06/05 18:10


大家好,  今天给大家介绍下我们在应用开发中最熟悉而陌生的朋友-----Context类 ,说它熟悉,是应为我们在开发中

时刻的在与它打交道,例如:Service、BroadcastReceiver、Activity等都会利用到Context的相关方法 ; 说它陌生,完全是

因为我们真正的不懂Context的原理、类结构关系。一个简单的问题是,一个应用程序App中存在多少个Context实例对象呢?

一个、两个? 在此先卖个关子吧。读了本文,相信您会豁然开朗的 。

Context,中文直译为“上下文”,SDK中对其说明如下:

Interface to global information about an application environment. This is an abstract class whose implementation

is provided by the Android system. It allows access to application-specific resources and classes, as well as up-calls

for application-level operations such as launching activities, broadcasting and receiving intents, etc

从上可知一下三点,即:

1、它描述的是一个应用程序环境的信息,即上下文。

2、该类是一个抽象(abstract class)类,Android提供了该抽象类的具体实现类(后面我们会讲到是ContextIml类)。

3、通过它我们可以获取应用程序的资源和类,也包括一些应用级别操作,例如:启动一个Activity,发送广播,接受Intent

信息 等。。

于是,我们可以利用该Context对象去构建应用级别操作(application-level operations) 。

一、Context相关类的相关继承关系

 

相关类介绍:

Context类    路径: /frameworks/base/core/java/android/content/Context.java

说明:  抽象类,提供了一组通用的API。

源代码(部分)如下:  

 

public abstract class Context {        ...        public abstract Object getSystemService(String name);  //获得系统级服务         public abstract void startActivity(Intent intent);     //通过一个Intent启动Activity         public abstract ComponentName startService(Intent service);  //启动Service         //根据文件名得到SharedPreferences对象         public abstract SharedPreferences getSharedPreferences(String name,int mode);        ...   } 

 

ContextIml.java类  路径 :/frameworks/base/core/java/android/app/ContextImpl.java

说明:该Context类的实现类为ContextIml,该类实现了Context类的功能。请注意,该函数的大部分功能都是直接调用

其属性mPackageInfo去完成,这点我们后面会讲到。

源代码(部分)如下

/**   * Common implementation of Context API, which provides the base   * context object for Activity and other application components.   */   class ContextImpl extends Context{       //所有Application程序公用一个mPackageInfo对象        /*package*/ ActivityThread.PackageInfo mPackageInfo;              @Override       public Object getSystemService(String name){           ...           else if (ACTIVITY_SERVICE.equals(name)) {               return getActivityManager();           }            else if (INPUT_METHOD_SERVICE.equals(name)) {               return InputMethodManager.getInstance(this);           }       }        @Override       public void startActivity(Intent intent) {           ...           //开始启动一个Activity            mMainThread.getInstrumentation().execStartActivity(               getOuterContext(), mMainThread.getApplicationThread(), null, null, intent, -1);       }   }


 

ContextWrapper类 路径 :\frameworks\base\core\java\Android\content\ContextWrapper.java

说明: 正如其名称一样,该类只是对Context类的一种包装,该类的构造函数包含了一个真正的Context引用,即ContextIml

对象。    源代码(部分)如下:

 

public class ContextWrapper extends Context {       Context mBase;  //该属性指向一个ContextIml实例,一般在创建Application、Service、Activity时赋值               //创建Application、Service、Activity,会调用该方法给mBase属性赋值        protected void attachBaseContext(Context base) {           if (mBase != null) {               throw new IllegalStateException("Base context already set");           }           mBase = base;       }       @Override       public void startActivity(Intent intent) {           mBase.startActivity(intent);  //调用mBase实例方法        }   }  

ContextThemeWrapper类 路径:/frameworks/base/core/java/android/view/ContextThemeWrapper.java

说明:该类内部包含了主题(Theme)相关的接口,即android:theme属性指定的。只有Activity需要主题,Service不需要主题,

所以Service直接继承于ContextWrapper类。

源代码(部分)如下:

public class ContextThemeWrapper extends ContextWrapper {        //该属性指向一个ContextIml实例,一般在创建Application、Service、Activity时赋值                 private Context mBase;       //mBase赋值方式同样有一下两种         public ContextThemeWrapper(Context base, int themeres) {               super(base);               mBase = base;               mThemeResource = themeres;        }           @Override        protected void attachBaseContext(Context newBase) {               super.attachBaseContext(newBase);               mBase = newBase;        }   }  


Activity类 、Service类 、Application类本质上都是Context子类, 更多信息大家可以自行参考源代码进行理解。


 

 

原创粉丝点击