Android应用程序生命周期 - Lifecycle of an Android Application

来源:互联网 发布:阶级固化 知乎 编辑:程序博客网 时间:2024/05/16 07:47

这篇文章已是几年前的,但是目前应该依然使用于各种不同进程被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 not directly 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 Activity, Service, 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基于运行于其中的组件和这些组件的状态把进程放入“重要性阶层”,重要性次序如下:
    A 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.
    一个前台进程是:持有一个Activity ,该活动是与用户进行交互(其onResume() 方法已被调用过)的顶层屏幕或一个正在运行的IntentReceiver (其 onReceiveIntent() 方法正在执行)。系统中只有少数这样的进程,系统只有在内存低至无法运行这些进程时,才会最后选择杀掉这些进程。通常此时设备达到一个内存分页调度状态,该动作用于保证用户交互是可响应的。
    A 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.
    一个可视进程是:持有一个用户组屏幕上可见的Activity,但不是位于前景(其 onPause() 方法已被调用过)。比如,当前景的活动已经显示,一个对话框出现,则之前的活动则在其之后可见。这类进程被认为非常重要,不会被杀掉,除非杀掉该进程是为了保持所有前台进程处于运行状态。
    A 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.
    一个服务进程是:持有一个通过startService() 方法启动的Service。尽管这些进程并非对用户可见,它们通常处理一些用户关心的事情(比如后台mp3播放器或后台网络数据上传下载),所有系统总是尽量保持这些进程运行,除非没有足够的内存保证所有前台进程和可视进程运行。
    A 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.
    一个后台进程是:持有一个当前对用户不可见的Activity (其onStop() 方法已被调用)。这些进程对用户的体验没有直接影响。如果其持有的活动的生命周期相关方法被正确实现(更多细节请看 Activity ),系统会在任何时候杀掉这些进程以回收内存供前三种进程使用。通常会有很多这类进程运行,他们被保存在一个LRU列表内,保证最新被用户看到的进程在系统内存过低时最晚被系统杀掉。
    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.
    一个空进程是:没有持有任何活动的应用程序组件。保持这样一个进程的唯一理由是在下一次一个应用程序的组件需要运行时作为缓存来加快启动速度。同样,系统会经常杀掉这些进程以在空的缓存进程和底层内核缓存之间保持系统资源平衡。
    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 Activity, Service, and IntentReceiver documentation 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 文档了解更多关于这些组件如何影响进程生命周期的细节。相关类的文档详细描述了它们如何影响其应用程序的生命周期。


原文地址:http://blog.sina.com.cn/s/blog_4fe2ba9001000b15.html

0 0
原创粉丝点击