Application详解

来源:互联网 发布:安卓6.0 移动数据开关 编辑:程序博客网 时间:2024/04/29 05:37

1. 官网介绍:

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.

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类是一个基类,这个基类的作用是为了获取整个应用程序的状态。你可以自己继承或实现这个类,当你要使用自己拓展的application类的时候,只要在manifest.xml中的<application>标签中name应用自己定义的类就行了,这样做的结果是:当你的应用程序或者包所在的进程创建的时候,这个类就会被实例化。

通常没有需要子类应用程序。在大多数情况下,静态单例可以更模块化的提供相同的功能。如果你需要一个全局的context(例如用来注册广播接受),当第一次构造实例使用Context.getApplicationContenxt()来获得一个context。

public void onConfigurationChanged (Configuration newConfig)

Added in API level 1

Called by the system when the device configuration changes while your component is running. Note that, unlike activities, other components are never restarted when a configuration changes: they must always deal with the results of the change, such as by re-retrieving resources.

At the time that this function has been called, your Resources object will have been updated to return resource values matching the new configuration.

For more information, read Handling Runtime Changes.

虽然你的组件任然在运行但当你的设备的配置变化后系统会调用这个方法。注意,不像activites,当configuration 改变的时候其他的组件不会restarted:他们常常需要处理改变的结果,例如,重新获取数据。

当该方法被调用的时候,resource将会被更新来适应新的configuration。

public void onCreate ()

Added in API level 1

Called when the application is starting, before any activity, service, or receiver objects (excluding content providers) have been created. Implementations should be as quick as possible (for example using lazy initialization of state) since the time spent in this function directly impacts the performance of starting the first activity, service, or receiver in a process. If you override this method, be sure to call super.onCreate().

    当你的应用程序启动的时候,在activity,service,或者receiver objects(包括content providers)被创建之前被调用.里面的实现应该尽可能的快(例如使用延时初始化),因为你花在这个方法的时间直接影响你在程序中启动第一个activity,service,或者receiver的性能。如果你重写了该方法,记得调用super.onCreate().

public void onLowMemory ()

Added in API level 1

This is called when the overall system is running low on memory, and actively running processes should trim their memory usage. While the exact point at which this will be called is not defined, generally it will happen when all background process have been killed. That is, before reaching the point of killing processes hosting service and foreground UI that we would like to avoid killing.

You should implement this method to release any caches or other unnecessary resources you may be holding on to. The system will perform a garbage collection for you after returning from this method.

Preferably, you should implement onTrimMemory(int) from ComponentCallbacks2 to incrementally unload your resources based on various levels of memory demands. That API is available for API level 14 and higher, so you should only use this onLowMemory() method as a fallback for older versions, which can be treated the same asonTrimMemory(int) with the TRIM_MEMORY_COMPLETE level.

该方法在系统处于低内存的时候被调用,并且活跃的进程都应该削减内存使用。虽然明确的定义在哪个点这个方法被调用,但是通常当所有的后台进程被killed的时候该方法会执行。因此,我们希望避免我们的hosting vervice 和 UI在该临界点直接被杀掉。

你应该实现该方法来是否内存或者其他的你holding on的没有必要的资源,系统将会执行垃圾回收在该方法被执行以后。

也行,你应该从ComponentCallbacks2 实现onTrimMemory(int)来递增根据不同级别来释放资源。在API level 14或者更高的版本使用,所以你应该在低版本中使用onLowMemory() ;

public void onTerminate ()

Added in API level 1

This method is for use in emulated process environments. It will never be called on a production Android device, where processes are removed by simply killing them; no user code (including this callback) is executed when doing so.

该方法只在模拟器上运行,在真机上不运行。


0 0