Android - 认识Context

6,372 阅读3分钟

app开发中,我们需要使用app的资源,比如文字、图片,Activity、Service或者broadcastReceiver等等。时常也会用到getApplicationContext()来获取一个Context对象。那么这个Context到底是什么呢?

我们一起来认识一下Android中的Context。本文主要内容如下图。

Android-认识Context.png

Context类简介

context含义有语境,上下文,背景,环境等等。 Context是维持Android程序中各组件能够正常工作的一个核心功能类。

Context是一个抽象类。它是一个 app 全局环境的“接口”,由 Android 系统提供继承类(例如Activity、Service、Application等)。它能连接到应用的资源,也能使用应用级的操作,比如启动activity,广播和接收intent。

应用程序中Context的总数目为: 总Context个数 = Activity个数 + Service个数 + 1(Application Context)

Context的子类

简单的继承关系示意

Android-Context-class-Inheritance.png

Context
├── ContextImpl
└── ContextWrapper
    ├── Application
    ├── ContextThemeWrapper
    │   └── Activity
    └── Service

从继承关系图中可以看出,Application类、Service类和Activity类都继承了Context类。应用程序被启动后,会为应用程序创建一个全局的Application对应的Context对象。ContextImpl类是Context的真正实现。ContextWrapper类是封装类。可以在不改变ContextImpl的情况下,为其增加一些自定义操作。ContextWrapper中的mBase实际上是一个ContextImpl对象。而ContextImpl类中的mOuterContext是一个Context对象,指向相对应的Activity或Service或Application。

ContextImpl

Context 是一个抽象类,子类 ContextImpl 实现了Context的方法;为Activity和其他应用组件提供基本的context对象。 ContextImpl.java (frameworks\base\core\java\android\app)

/**
 * Common implementation of Context API, which provides the base
 * context object for Activity and other application components.
 */
class ContextImpl extends Context { /*...*/ }

ContextWrapper

Wrapper 有封装的意思;ContextWrapper是Context的封装类 。这里使用了装饰者模式,构造方法中传入了一个Context 实例。ContextWrapper持有ContextImpl对象。可以在不改变ContextImpl的情况下增加一些操作。

ContextWrapper.java (frameworks\base\core\java\android\content)

/**
 * 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.
 */
public class ContextWrapper extends Context {
    Context mBase;

    public ContextWrapper(Context base) {
        mBase = base;
    }
    // ...
}

具体操作中,Application类、Activity和Service类与ContextImpl产生交集。

ContextThemeWrapper

允许在封装的context中修改主题(theme)

ContextThemeWrapper.java (frameworks\base\core\java\android\view)

/**
 * A ContextWrapper that allows you to modify the theme from what is in the
 * wrapped context.
 */
public class ContextThemeWrapper extends ContextWrapper { /* ... */ }

其中提供了关于theme的方法,app开发中 android:theme 与此有关。相同的代码,相同的调用,使用不同的 theme 会有不同的效果。

getApplicationContext() 和 getBaseContext()

public class ContextWrapper extends Context {
    Context mBase;
    ......
    @Override
    public Context getApplicationContext() {
        return mBase.getApplicationContext();
    }
    ......
    /**
     * @return the base context as set by the constructor or setBaseContext
     */
    public Context getBaseContext() {
        return mBase;// Don't use getBaseContext(), just use the Context you have.
    }
    ......
}
getApplicationContext() = android.app.Application@39d42b0e
getBaseContext() = android.app.ContextImpl@1f48c92f

getApplicationContext() 从application取得context。getBaseContext() 从实现类ContextImpl那得来。

Context子类创建流程

Application的创建流程

我们把关注点先放在application上,暂时忽略源码中的其他信息。

Android-Context-Application-flow.png

流程描述:

LoadedApk先通过classLoaderloadClass(className)获取application的class,再通过clazz.newInstance()创建Application实例。接着调用app.attach(context)方法完成初始化。 Application的attach方法里调用了自己的attachBaseContext(context), 把第一步创建的ContextImpl实例赋值给ContextWrappermBase成员变量。 到此Application实例创建就完成了。

Activity的创建 - performLaunchActivity

这里关注的是Activity的实例化以及之前的一些准备过程。

Android-Context-performLaunchActivity-flow.png

流程简析:

主要关注ActivityThreadperformLaunchActivity方法。通过ContextImplcreateActivityContext获得一个ContextImpl实例,称为appContextInstrumentationnewActivity返回了一个Activity实例。LoadedApk.makeApplication可以获得当前的application,如果当前没有则新建一个application。最后通过ContextImpl.setOuterContext和Activity的attach方法,将ContextImpl实例与Activity实例关联到一起。

使用

自定义Application

新建一个MyApplication类,继承自Application

import android.app.Application;
import android.content.Context;

public class MyApplication extends Application {
    private static Context context;

    public static Context getMyContext() {
        return context;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        context = getApplicationContext();
    }
}

在Manifest中使用MyApplication;

    <application
        android:name="com.rust.aboutview.MyApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
    ......

即可在不同的地方调用 getMyContext() 方法

MyApplication.getMyContext()

参考

blog.csdn.net/yanbober/ar…

xujiaojie.github.io/2017/09/18/…

xujiaojie.github.io/2017/09/18/…

juejin.cn/post/684490…