0%

Android之Context、ContextImp、ContextWrapper

Context、ContextImpl、ContextWrapper关系?

  • Context是行为的抽象接口。
  • ContextImpl是Android平台上的具体实现。
  • ContextWrapper是装饰器可以修改Context的行为而不修改原始的Context对象。

ContextWrapper装饰Context有什么具体场景?

比如遇到Receiver的时候,ReceiverRestrictedContext就能够限制Receiver调用startService,让应用程序的Receiver不能够启动Service。通过实现装饰器,能够提供各种各样的装饰,并且也为Android应用程序组件提供了应用环境。

Context类继承关系

ContextWrapper的mBase是哪个对象?ContextImpl的mOuterContext是哪个对象?

  • Activity通过ContextWrapper的成员mBase来引用了一个ContextImpl对象,这样,Activity组件以后就可以通过这个ContextImpl对象来执行一些具体的操作(启动Service等)。
  • 同时ContextImpl类又通过自己的成员mOuterContext引用了与它关联的Activity,这样ContextImpl类也可以操作Activity。

ContextThemeWrapper使用场景?

可以传一个主题样式id过来,用style中的各个属性,覆盖原有Context中的属性,在动态替换主题的时候会比较有用。