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描述了一个应用程序环境的全局信息。
 * 它是一个抽象类,由系统提供它的实现方法。
 * 经过它能够访问application中的资源和类,进行一些应用级别的操做,好比启动activity、broadcasting、
 * 接受intent等。
 */

Context相关类的继承方式

一、Context是一个抽象类,ContextImpl是其实现。全部有关Context的调用实际上都是ContextImpl实现的。java

二、Context有一个子类是ContextWrapper,ContextWrapper中持有一个ContextImpl的实例。对ContextWrapper有关Context的接口调用ContextWrapper都会经过ContextImpl来实现。这里使用到了装饰者模式。有关装饰者模式,具体能够参考http://www.javashuo.com/article/p-vmxgehma-hy.htmlapp

三、ContextWrapper有一个子类是ContextThemeWrapper,顾名思义它是跟界面相关的ContextWrapper。ide

四、Application、Service继承于ContextWrapper。Activity继承于ContextThemeWrapper。spa

Context和Activity的绑定过程

ActivityManagerService -> ActivityThread的main()程序主入口 -> 建立Handler -> 接受事件(好比LAUNCH_ACTIVITY、BIND_APPLICATION等) -> LaunchAtivity为例,handleLaunchActivity() -> performLaunchActivity()中.net

1.经过Instrumentation构造一个Activity对象code

2.经过Instrumentation构造一个Application对象orm

3.经过createBaseContextForActivity()建立ContextImpl实例,经过setOuterContext和Activity对象绑定对象

4.获取Configuration对象blog

5.经过attach将context保存到Activity中继承

 

PS:ActivityManagerService借助ActivityStack是来把全部的Activity按照后进先出的顺序放在一个堆栈中;对于每个应用程序来讲,都有一个ActivityThread来表示应用程序的主进程,而每个ActivityThread都包含有一个ApplicationThread实例,它是一个Binder对象,负责和其它进程进行通讯。

相关文章
相关标签/搜索