官方描述:Base class for those who need to maintain global application state. You can provide your own implementation by specifying its name in your AndroidManifest.xml's <application> tag, which will cause that class to be instantiated for you when the process for your application/package is created.
为应用程序提供全局变量的基础类,你可以自己实现application通过在AndroidManifest.xml的<application>标签指定它的名字,此举会导致在应用程序的进程被建立时此类被实例化。
There is normally no need to subclass Application. In most situation, static singletons can provide the same functionality in a more modular way. If your singleton needs a global context (for example to register broadcast receivers), the function to retrieve it can be given a Context
which internally uses Context.getApplicationContext()
when first constructing the singleton.
实现Application的子类不是必须的。在大多数情况下,静态单例模式可以以一个模块化的方式实现相同功能。如果你的单例模式需要一个全局context,恢复使用Application可以在你第一次创建单例时通过使用Context.getApplicationContext()
获取一个Context。
一个Application是一个进程,Application为整个程序提供Context。
Application类继承
- java.lang.Object:根类
- android.content:包含类用来在设备上获取和发布数据。
android.content.ContextWrapper:Context的代理,简单的代表它的所有需求到另一个Context.Wrapper:包装.
- Content sharing:用于在不同应用组件间分享内容。
-
ContentProvider
andContentResolver:用于在应用程序间管理和发布固有数据。
Intent and
IntentFilter:在不同应用组件间交付结构化消息,允许组件去初始化其他组件和返回结果。
-
Package management:获取安卓包的信息,包括activities, permissions, services, signatures, and providers
Resource management:检索与应用相关的资源ID,例如strings, drawables, media, and device configuration details。
Context:获取应用程序环境的全局信息,由系统提供实现的抽象类,它允许获取应用具体的资源,并可实现应用程序级别的操作,例如启动活动,广播和接受intents
- Content sharing:用于在不同应用组件间分享内容。
- android.content.ContextWrapper:Context的代理。
android.app.Application
由Application类继承可以看到,Application类继承自ContextWrapper类,而ContextWapper类继承自Context类;
Context类主要用于描述应用程序环境和获取应用程序资源和类,以及应用程序级别的操作。
四大组件活动,广播,服务,内容提供器;这些组件建立后,应用程序才会建立,应用程序只是一个概念,用于包含组件。
android.content.Context |