当前位置:   article > 正文

android application 生命周期_android application 销毁生命周期

android application 销毁生命周期

在Android中进程按优先级可以分为五类,优先级从高到低排列:
前台进程 该进程包含正在与用户进行交互的界面组件,比如一个Activity
可视进程 该进程中的组件虽然没有和用户交互,但是仍然可以被看到
服务进程 该进程包含在执行后台操作的服务组件,比如播放音乐的进程
后台进程 该进程包含的组件没有与用户交互,用户也看不到
空进程 没有任何界面组件、服务组件,或触发器组件**
Android系统是进程托管的,也就是说进程都是由系统来管理,系统会按照特定的算来来回收这些进程。在回收中秉承几个原则
1. 尽量延长进程的生命周期,不到必须的情况下不会回收,因为系统回收进程会影响用户体验
2. 按优先级从低到高进行回收
3. 同等优先级的进程越近使用越晚回收。
进程过一段时间后是会被回收的,但要遵循上面的这些原则,播放音乐的这个进程的优先级还是比较高的,所以被莫名其妙地回收的可能性不大,在播放音乐时平白无故地停止这样的情况很少对吧?service和application的生命周期有关,只要进程被回收,那么它所占用的所有资源将被回收。

下面是详细介绍:
转自:http://blog.sina.com.cn/s/blog_4fe2ba9001000b15.html
Lifecycle of an Android Application
Android应用程序生命周期

  In most cases, every Android application runs in its own Linux process. This process is created for the application when some of its code needs to be run, and will remain running until it is no longer needed and the system needs to reclaim its memory for use by other applications.

An important and unusual feature of Android is that an application process's lifetime is notdirectly controlled by the application itself. Instead, it is determined by the system through a combination of the parts of the application that the system knows are running, how important these things are to the user, and how much overall memory is available in the system.

 通常情况下,每个Android应用程序运行在独立的Linux进程内。该进程在应用的某些代码需要运行时创建并保持运行,直到其不再需要运行而且系统需要回收其内存以供其他应用使用。Android一个重要且不同寻常的特点是一个应用程序进程的生命期并非由应用本身直接控制。作为替换,由系统综合多种因素决定其生命期:应用正在运行的部分,对用户的重要程度,以及系统还有多少可用内存。

  It is important that application developers understand how different application components (in particular ActivityService, and IntentReceiver) impact the lifetime of the application's process. Not using these components correctly can result in the system killing the application's process while it is doing important work.

  应用程序开发人员需要理解应用的组件(Activity,Service,和 IntentReceiver)对应用程序进程的生命期有不同的影响,这一点非常重要。对组件的不合理使用可能导致系统在应用执行重要工作期间杀掉应用进程。

  A common example of a process lifecycle bug is an IntentReceiver that starts a thread when it receives an Intent in its onReceiveIntent() method, and then returns from the function. Once it returns, the system considers that IntentReceiver to be no longer active, and thus its hosting process no longer needed (unless other application components are active in it). Thus, it may kill the process at any time to reclaim memory, terminating the spawned thread that is running in it. The solution to this problem is to start a Service from the IntentReceiver, so the system knows that there is still active work being done in the process.

  一个常见的进程生命周期Bug的例子是:一个IntentReceiver收到一个Intent并在其onReceiveIntent() 方法中启动一个线程,然后线程返回。一旦线程返回,系统认为该InentReceiver不再活动,因此其宿主进程不再需要(除非其他应用组件活跃其中)。因此,系统可能在任何时候杀掉该进程以回收内存,终止其衍生的线程。该问题的解决方法是从IntentReceiver启动一个Service,希望会知道该进程还有活动工作需要处理。

  To determine which processes should be killed when low on memory, Android places them into an "importance hierarchy" based on the components running in them and the state of those components. These are, in order of importance:

  为了在内存不够时决定杀掉那些进程,Android基于运行于其中的组件和这些组件的状态把进程放入“重要性阶层”,重要性次序如下:

  1. foreground process is one holding an Activity at the top of the screen that the user is interacting with (its onResume() method has been called) or an IntentReceiver that is currently running (its onReceiveIntent() method is executing). There will only ever be a few such processes in the system, and these will only be killed as a last resort if memory is so low that not even these processes can continue to run. Generally at this point the device has reached a memory paging state, so this action is required in order to keep the user interface responsive.

  2.  一个前台进程是:持有一个Activity ,该活动是与用户进行交互(其onResume() 方法已被调用过)的顶层屏幕或一个正在运行的IntentReceiver (其 onReceiveIntent() 方法正在执行)。系统中只有少数这样的进程,系统只有在内存低至无法运行这些进程时,才会最后选择杀掉这些进程。通常此时设备达到一个内存分页调度状态,该动作用于保证用户交互是可响应的。

  3. visible process is one holding an Activity that is visible to the user on-screen but not in the foreground (its onPause() method has been called). This may occur, for example, if the foreground activity has been displayed with a dialog appearance that allows the previous activity to be seen behind it. Such a process is considered extremely important and will not be killed unless doing so is required to keep all foreground processes running.

  4. 一个可视进程是:持有一个用户组屏幕上可见的Activity,但不是位于前景(其 onPause() 方法已被调用过)。比如,当前景的活动已经显示,一个对话框出现,则之前的活动则在其之后可见。这类进程被认为非常重要,不会被杀掉,除非杀掉该进程是为了保持所有前台进程处于运行状态。

  5. service process is one holding a Service that has been started with the startService()method. Though these processes are not directly visible to the user, they are generally doing things that the user cares about (such as background mp3 playback or background network data upload or download), so the system will always keep such processes running unless there is not enough memory to retain all foreground and visible process.

  6. 一个服务进程是:持有一个通过startService() 方法启动的Service。尽管这些进程并非对用户可见,它们通常处理一些用户关心的事情(比如后台mp3播放器或后台网络数据上传下载),所有系统总是尽量保持这些进程运行,除非没有足够的内存保证所有前台进程和可视进程运行。

  7. background process is one holding an Activity that is not currently visible to the user (its onStop() method has been called). These processes have no direct impact on the user experience. Provided they implement their activity lifecycle correctly (see Activity for more details), the system can kill such processes at any time to reclaim memory for one of the three previous processes types. Usually there are many of these processes running, so they are kept in an LRU list to ensure the process that was most recently seen by the user is the last to be killed when running low on memory.

  8. 一个后台进程是:持有一个当前对用户不可见的Activity (其onStop() 方法已被调用)。这些进程对用户的体验没有直接影响。如果其持有的活动的生命周期相关方法被正确实现(更多细节请看 Activity ),系统会在任何时候杀掉这些进程以回收内存供前三种进程使用。通常会有很多这类进程运行,他们被保存在一个LRU列表内,保证最新被用户看到的进程在系统内存过低时最晚被系统杀掉。

  9. An empty process is one that doesn't hold any active application components. The only reason to keep such a process around is as a cache to improve startup time the next time a component of its application needs to run. As such, the system will often kill these processes in order to balance overall system resources between these empty cached processes and the underlying kernel caches.

  10. 一个空进程是:没有持有任何活动的应用程序组件。保持这样一个进程的唯一理由是在下一次一个应用程序的组件需要运行时作为缓存来加快启动速度。同样,系统会经常杀掉这些进程以在空的缓存进程和底层内核缓存之间保持系统资源平衡。

  When deciding how to classify a process, the system picks the most important level of all the components currently active in the process. See the ActivityService, and IntentReceiverdocumentation for more detail on how each of these components contribute to the overall lifecycle of a process. The documentation for each of these classes describes in more detail how they impact the overall lifecycle of their application.

  当决定如何对进程分类时,系统挑选在所有当前运行于进程中的组件中填写最为重要的一类。看Activity,Service和 IntentReceiver 文档了解更多关于这些组件如何影响进程生命周期的细节。相关类的文档详细描述了它们如何影响其应用程序的生命周期。


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/589781
推荐阅读
相关标签
  

闽ICP备14008679号