Android中的Context对象

相关的类

Context

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.

就是说Context是用来处理应用级别的事务的,比如想调用resources里的图片、文字,又或是想用SharedPreferences来存取一些配置等,这些都是属于应用级别的资源,通过Context可以对它们进行调用。又比如想启动另外一个Activity或者是一个Service,也需要通过Context来实现。
下面是Context的部分源码(source code):

public abstract class Context {    ...    /** 用于获取应用的资源,如字符串,图片等 */    public abstract Resources getResources();    /** 启动Activity */    public abstract void startActivity(Intent intent);    /** 启动Service */    public abstract ComponentName startService(Intent service);    /** 获取SharedPreferences */    public abstract SharedPreferences getSharedPreferences(String name, int mode);    ...}

通过源码可以看出,Context是一个抽象类,里面定义了一堆方法,但是都没有具体的实现。按照官方文档中所说具体实现会由android系统提供的,一个例子就是下面提到的ContextImpl。

ContextImpl

Common implementation of Context API, which provides the base context object for Activity and other application components.

实现Context接口的类,为Activity和其他组件提供了基础的Context对象。

下面是ContextImpl的部分源码(source code):

class ContextImpl extends Context {    ...    @Override    public void startActivity(Intent intent, Bundle options) {        warnIfCallingFromSystemProcess();        if ((intent.getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {            throw new AndroidRuntimeException(                    "Calling startActivity() from outside of an Activity "                    + " context requires the FLAG_ACTIVITY_NEW_TASK flag."                    + " Is this really what you want?");        }        mMainThread.getInstrumentation().execStartActivity(                getOuterContext(), mMainThread.getApplicationThread(), null,                (Activity) null, intent, -1, options);    }    ...}

通过源码看出,ContextImpl实现了Context接口里的方法。

ContextWrapper

Proxying implementation of Context that simply delegates all of its calls to another Context. Can be subclassed to modify behavior without changing the original Context.

ContextWrapper的所有方法的都委托给了委托给了一个Context类的实现(例如ContextImpl)。这是采用了装饰模式。

下面是ContextWrapper的部分源码(source code):

public class ContextWrapper extends Context {    Context mBase;    /** 绑定一个mBase的Context对象,调用方法时会直接委托给mBase来处理 */    protected void attachBaseContext(Context base) {        if (mBase != null) {            throw new IllegalStateException("Base context already set");        }        mBase = base;    }    ...    @Override    public Resources getResources() {        return mBase.getResources();    }    @Override    public void startActivity(Intent intent) {        mBase.startActivity(intent);    }    ...}

通过源码看出,ContextWrapper持有了一个mBase的Context对象,然后代理了mBase中的方法。mBase就是一个ContextImpl的实例。

ContextThemeWrapper

A ContextWrapper that allows you to modify the theme from what is in the wrapped context.

ContextThemeWrapper包含了主题相关的一些功能,Activity就是继承自ContextThemeWrapper,而Service是继承自ContextWrapper。

参考文档

Android中Context详解 ---- 你所不知道的Context
Context, What Context?
Android应用程序窗口(Activity)的运行上下文环境(Context)的创建过程分析

更多相关文章

  1. Android 闹钟源码
  2. Android SDK Manager 更新失败的解决方法
  3. 编译cubieboard android 源码过程详解之(六):pack
  4. Android三种方法设置ImageView的图片
  5. Android中使用Gallery_tabhost来实现标签效果图源码
  6. Android中启动动画源码讲解
  7. Android 源码 修改系统默认横屏
  8. 45个android实例源码

随机推荐

  1. Android(安卓)通过Android(安卓)SDK Mana
  2. Android渗透测试Android渗透测试入门教程
  3. android系统自带的样式(@android:style)
  4. 50个Android开发人员必备UI效果源码
  5. Android跨进程通信IPC之6——Binder框架
  6. android 关于google刘海屏的解决方案
  7. Android中gravity与layout_gravity的区别
  8. Android开发——控件基础(五)RadioButton组
  9. android中的六大布局
  10. Android腾讯微薄客户端开发教程汇总