各个Context 类的关系图

Context 作用是啥
看它能做啥,看看主要提供了哪些接口了。
四大组件相关 | 包括启动 Activity、Broadcast、Service,获取 ContentResolver 等 |
![]() |
获取系统/应用资源 | 包括 AssetManager、PackageManager、Resources、System Service 以及 color、string、drawable 等 |
![]() |
获取应用相关信息 |
![]() |
|
文件相关 |
![]() |
|
数据库相关 | 包括打开数据库、删除数据库、获取数据库路径等 |
![]() |
其它 | 比如设置 ComponentCallbacks,即监听配置信息改变、内存不足等事件的发生 |
![]() |
ContextImpl 和 ContextWrapper 有啥区别?
Class | Diff |
---|---|
ContextImpl | 真正实现了Context中所有的函数 (真正厉害的!) |
ContextWrapper | Context 的代理类,所有的操作都是通过内部成员 mBase 完成的 |
ContextThemeWrapper | 内部包含了与主题相关的接口,有自己的 Theme 以及 Resource,并且 Resource 可以传入自己的配置初始化 |
Activity、Service、Application 各自的Context有啥区别?
API | |
---|---|
Activity | 调用 createBaseContextForActivity |
Service/Application | 调用 createAppContext |
Provider | 调用 createPackageContext |
BroadcastReceiver | 直接从 Application.getBaseContext() 来获取 ContextImpl 对象 |
网友评论