android中的Context(android内核学习记录)

1、Context是什么?

一个Context意味着一个场景,一个场景就是用户和操作系统交互的过程。比如你打电话时,场景包括你打电话的界面,以及隐藏在界面后的数据。

Context到底是什么?
一个Activity是一个Context,一个Service也是一个Context。
我们先不看代码,而从语义的角度理解Context,Android程序员把场景抽象成Context类,它们认为用户和操作系统的每一次交互都是一个场景。比如打电话,发短信等这都是有界面的场景,而有些没有界面的场景:比如说Service。

这里写图片描述

从代码中可以看出。Activity类基于Context,而Service也是基于Context,Activity除了基于Context类外,还实现了一些其他的接口。从设计者的角度来讲,interface仅仅是某些功能,而extends才是类的本质。也就是说一个Activity是一个Context,一个Service也是一个Context。

2、Context相关类的继承

这里写图片描述
Context本身是一个纯抽象类。

1、ContextWrapper

为了使用方便,定义了ContextWrapper类,就是Context的包装类
Context的部分代码如下:

 public ContextWrapper(Context base) {
        mBase = base;
    }
    protected void attachBaseContext(Context base) {
        if (mBase != null) {
            throw new IllegalStateException("Base context already set");
        }
        mBase = base;
    }
    public Context getBaseContext() {
        return mBase;
    }
    @Override
    public AssetManager getAssets() {
        return mBase.getAssets();
    }
    @Override
    public Resources getResources()
    {
        return mBase.getResources();
    }
    @Override
    public PackageManager getPackageManager() {
        return mBase.getPackageManager();
    }

2、ContextThemeWrapper

ContextThemeWrapper其内部包含了主题相关的接口。这里所说的主题就是在AndroidMainfest.xml文件中配置的android:theme。它可以是为Application或者Activity元素指定的主题。当然,Activity需要主题,而Service没有界面,运行在后台的,因此Service直接继承于ContextWrapper。
ContextThemeWrapper的部分源码:

public class ContextThemeWrapper extends ContextWrapper {
    private int mThemeResource;
    private Resources.Theme mTheme;
    private LayoutInflater mInflater;
    private Configuration mOverrideConfiguration;
    private Resources mResources;
    public ContextThemeWrapper() {
        super(null);
    }
    public ContextThemeWrapper(Context base, int themeres) {
        super(base);
        mThemeResource = themeres;
    }
    @Override protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(newBase);
    }
    public void applyOverrideConfiguration(Configuration overrideConfiguration) {
        if (mResources != null) {
            throw new IllegalStateException("getResources() has already been called");
        }
        if (mOverrideConfiguration != null) {
            throw new IllegalStateException("Override configuration has already been set");
        }
        mOverrideConfiguration = new Configuration(overrideConfiguration);
    }

    @Override
    public Resources getResources() {
        if (mResources != null) {
            return mResources;
        }
        if (mOverrideConfiguration == null) {
            mResources = super.getResources();
            return mResources;
        } else {
            Context resc = createConfigurationContext(mOverrideConfiguration);
            mResources = resc.getResources();
            return mResources;
        }
    }

    @Override public void setTheme(int resid) {
        mThemeResource = resid;
        initializeTheme();
    }
}

3、一个应用程序中包含多少个Context对象?

Activity的个数+Service个数+1(Application)

4、Context在哪里创建?

一个应用程序在客户端都是从ActivityThread类开始的,创建Context也是在该类中完成具体创建ContextImpl类的地方一共有7处:

  1. 在PackageInfo.makeApplication();
  2. 在performLaunchActivity();
  3. 在handleCreateBackupAgent();
  4. 在handleBindApplication();—–有两处;
  5. 在handleCreateService();
  6. 在attach()方法中;

5、Applicatoin,Activity,Service对应的Context

1、Application对应的Context

每一个应用程序在启动的时候都会创建一个Application对象,默认的Applicatin就是应用程序的包名。用户可以重载Application。方法是在AndroidManifest.xml文件的Application标签中声明一个新的Application名称,然后在源码中添加该类的名称。

2、Activity对象的Context

启动Activity时,AmS通过IPC调用到ActivityThread的scheduleLaunchActivity()方法,然后调用到handlerLaunchActivity,最后调用到performLaunchActivity(),在该方法中创建了ContextImpl的代码如下:

ContextImple appContext=new ContextImpl();
appContext.init(r.packageInfo,r.token,this);
appContext.setOuter(activity);
ActivityInfo aInfo=r.activityInfo;
if(r.packageInfo==null){
    r.packageInfo=getPackageInfo(aInfo.application);
    Context.CONTEXT_INCLUDE_CODE;
}

即在performLaunchActivity()开始执行,首先为r.packageInfo变量赋值,packageInfo()方法执行的逻辑和getPackageInfoNoChecked()基本相同。所以,r.packageInfo对象的PackageInfo对象和Application对应的packageInfo对象是同一个对象。

3、Service对象的Context

Service对象Context与Activity创建的过程一样。我们平常说Service就是没有界面的Activity。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值