Context

本文深入探讨了Android应用中的Context概念,解释了Context的继承关系,并详细阐述了ContextWrapper、ContextThemeWrapper和ContextImpl类的作用。此外,文章还介绍了如何在Application、Activity和Service中创建Context对象及它们之间的区别。

Context被译为上下文,可理解为场景,一个场景就是用户和操作系统交互的一种过程,如打电话场景包括电话对应的界面,还有隐藏在界面后的数据。Context是一个抽象类,Activity基于Context,Service也基于Context。Activity除了基于Context,还实现了一些其他重要的接口。从设计的角度看,interface仅仅是某些功能,而extends才是类的本质,即Activity的本质是一个Context,其所实现的其他接口只是为了扩充Context的功能而已,扩充之后的类称之为一个Activity或者Service。

Context继承关系:

ContextWrapper类:这只是一个包装而已,ContextWrapper构造函数必须包含一个真正的Context引用,同时ContextWrapper中提供了attachBaseContext用于给ContextWrapper对象中指定真正的Context对象,调用ContextWrapper的方法都会被转向其所包含的真正的Context对象。

ContextThemeWrapper类:其内部包含了与主题相关的接口,这里说的主题是指在AndroidMainifest.xml中通过android:theme为Application元素或者Activity元素指定的主题。

当然只有Activity需要主题,Service是不需要主题的。因为Service是没有界面的后台场景,所有Service直接继承于ContextWrapper。

ContextImpl类:该类真正实现了Context中所有的函数,应用程序中调用的各种Context类的方法,其实现均来自该类。


Application中的Context


Activity中的Context


Service中的Context


上面三种情况下的Context对象的创建过程基本是相同的,包括的代码的结构也十分类似,它们都对应了相同的PackageInfo。不同的是针对Application,Activity,Service使用了不同的数据对象。

实际一个应用程序中包含的Context个数 = Activity个数 + Service个数 + 1(Application)


应用程序包含多个ContextXmpl对象,而其内部变量mPackageInfo却指向同一个PackageInfo对象。这意味着ContextXmpl是个轻量级的类,PackageInfo是个重量级的类,ContextXmpl中的大多数进行包操作的重量级函数实际上都是转向了mPackageInfo对象相应的方法。



03-08
### Context in Programming or IT Systems In programming and IT systems, context refers to the environment and conditions under which certain operations are performed. This concept encompasses several aspects depending on the specific area within computing: #### Execution Context Execution context pertains specifically to how code executes at runtime. Each function call creates its own execution context that includes variables, scope chain, and this binding[^1]. For instance, when discussing JavaScript, each time a script starts running or enters a new function, an execution context is created. #### Thread Context Thread context involves all information associated with a particular thread's state including registers, stack pointers, program counter, etc.[^3]. When switching between threads, saving and restoring these contexts ensures continuity of operation without data loss or corruption. #### Security Context Security context defines what actions can be taken by processes based on their permissions level. It plays a critical role in determining access control policies applied to resources like files, network connections, hardware devices, among others. #### Application Context Application context represents settings relevant to applications such as configuration parameters, user preferences, session states, etc., ensuring consistent behavior across different parts of software while maintaining isolation from other programs sharing similar environments. ```python def example_function(): # An execution context is formed here containing local variable definitions, # parameter bindings, parent scopes references (scope chain), along with 'this' value. pass ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值