Android降低应用在后台干掉的概率

来源:互联网 发布:中国人工智能 知乎 编辑:程序博客网 时间:2024/05/23 18:57

背景

在应用运行过程中,难免切到后台,可是有些情况仍然需要应用在后台运行,为我们采集或更新数据,基于这个情景,需要找到合适的方案,禁止后台应用被干掉,或者降低被干掉的概率,保证应用能够在后台正常的采集数据,使应用更好的服务于客户,并且也提高了应用的质量品阶。

面对这样的问题自己就着手从官方文档找寻问题的答案。

a.Android应用的进程

When an application component starts andthe application does not have any other components running, the Android systemstarts a new Linux process for the application with a single thread of execution.By default, all components of the same application run in the same process andthread (called the "main" thread). If an application component startsand there already exists a process for that application (because anothercomponent from the application exists), then the component is started withinthat process and uses the same thread of execution.

当某个应用组件启动且该应用没有运行其他任何组件时,Android 系统会使用单个执行线程为应用启动新的 Linux 进程。默认情况下,同一应用的所有组件在相同的进程和线程(称为“主”线程)中运行。 如果某个应用组件启动且该应用已存在进程(因为存在该应用的其他组件),则该组件会在此进程内启动并使用相同的执行线程。

 

By default, all components of the sameapplication run in the same process and most applications should not changethis.

默认情况下,同一应用的所有组件均在相同的进程中运行,且大多数应用都不会改变这一点。

b.内存不足时

Android might decide to shut down a processat some point, when memory is low and required by other processes that are moreimmediately serving the user. Application components running in the processthat's killed are consequently destroyed. A process is started again for thosecomponents when there's again work for them to do.

如果内存不足,而其他为用户提供更紧急服务的进程又需要内存时,Android 可能会决定在某一时刻关闭某一进程。在被终止进程中运行的应用组件也会随之销毁。当这些组件需要再次运行时,系统将为它们重启进程。

When deciding which processes to kill, theAndroid system weighs their relative importance to the user.

决定终止哪个进程时,Android系统将权衡它们对用户的相对重要程度。

c.清理进程规则

The Android system tries to maintain anapplication process for as long as possible, but eventually needs to remove oldprocesses to reclaim memory for new or more important processes. To determinewhich processes to keep and which to kill, the system places each process intoan "importance hierarchy" based on the components running in theprocess and the state of those components. Processes with the lowest importanceare eliminated first, then those with the next lowest importance, and so on, asnecessary to recover system resources.

Android 系统将尽量长时间地保持应用进程,但为了新建进程或运行更重要的进程,最终需要移除旧进程来回收内存。为了确定保留或终止哪些进程,系统会根据进程中正在运行的组件以及这些组件的状态,将每个进程放入“重要性层次结构”中。必要时,系统会首先消除重要性最低的进程,然后是重要性略逊的进程,依此类推,以回收系统资源。

 

There are five levels in the importancehierarchy. The following list presents the different types of processes inorder of importance (the first process is most important and is killed last):

重要性层次结构一共有 5 级。以下列表按照重要程度列出了各类进程(第一个进程最重要,将是最后一个被终止的进程)。

d.进程的等级

1).前台进程

A process that is required for what theuser is currently doing. A process is considered to be in the foreground if anyof the following conditions are true:

-It hosts an Activity that the user isinteracting with (the Activity's onResume() method has been called).

-It hosts a Service that's bound to theactivity that the user is interacting with.

-It hosts a Service that's running "inthe foreground"—the service has called startForeground().

-It hosts a Service that's executing one ofits lifecycle callbacks (onCreate(), onStart(), or onDestroy()).

-It hosts a BroadcastReceiver that'sexecuting its onReceive() method.

 

用户当前操作所必需的进程。如果一个进程满足以下任一条件,即视为前台进程:

(1).它拥有者正在与用户交互的Activity(已调用Activity的onResume()方法)。

(2).它拥有一个服务,这个服务绑定到正在与用户交互的Activity上。

(3).它拥有一个在“前台”运行的服务(在服务中调用了startForeground())。

(4).它拥有一个正在执行一个生命周期回调的Service(onCreate()、onStart()或onDestory())。

(5).它拥有一个正在执行其onReceive()方法的BroadcastReceiver。

 

Generally, only a few foreground processesexist at any given time. They are killed only as a last resort—if memory is solow that they cannot all continue to run. Generally, at that point, the devicehas reached a memory paging state, so killing some foreground processes isrequired to keep the user interface responsive.

通常,在任意给定时间前台进程都为数不多。只有在内存不足以支持它们同时继续运行这一万不得已的情况下,系统才会终止它们。

2).可见进程

A process that doesn't have any foregroundcomponents, but still can affect what the user sees on screen. A process is consideredto be visible if either of the following conditions are true:

-It hosts an Activity that is not in theforeground, but is still visible to the user (its onPause() method has beencalled). This might occur, for example, if the foreground activity started adialog, which allows the previous activity to be seen behind it.

-It hosts a Service that's bound to avisible (or foreground) activity.

 

没有任何前台组件、但仍会影响用户在屏幕上所见内容的进程。 如果一个进程满足以下任一条件,即视为可见进程:

(1).它拥有一个不在前台但是仍然对用户可见的Activity(已调用其onPause() 方法)。例如,如果前台 Activity 启动了一个对话框,允许在其后显示上一 Activity,则有可能会发生这种情况。

(2).它拥有一个绑定可见(或前台)Activity的服务。

 

A visible process is considered extremelyimportant and will not be killed unless doing so is required to keep allforeground processes running.

可见进程被视为是极其重要的进程,除非为了维持所有前台进程同时运行而必须终止,否则系统不会终止这些进程。

3).服务进程

A process that is running a service thathas been started with the startService() method and does not fall into eitherof the two higher categories. Although service processes are not directly tiedto anything the user sees, they are generally doing things that the user caresabout (such as playing music in the background or downloading data on thenetwork), so the system keeps them running unless there's not enough memory toretain them along with all foreground and visible processes.

 

正在运行已使用startService() 方法启动的服务且不属于上述两个更高类别进程的进程。尽管服务进程与用户所见内容没有直接关联,但是它们通常在执行一些用户关心的操作(例如,在后台播放音乐或从网络下载数据)。因此,除非内存不足以维持所有前台进程和可见进程同时运行,否则系统会让服务进程保持运行状态。

4).后台进程

A process holding an activity that's notcurrently visible to the user (the activity's onStop() method has been called).These processes have no direct impact on the user experience, and the systemcan kill them at any time to reclaim memory for a foreground, visible, orservice process. Usually there are many background processes running, so theyare kept in an LRU (least recently used) list to ensure that the process withthe activity that was most recently seen by the user is the last to be killed.

 

包含目前对用户不可见的Activity 的进程(已调用 Activity 的 onStop() 方法)。这些进程对用户体验没有直接影响,系统可能随时终止它们,以回收内存供前台进程、可见进程或服务进程使用。通常会有很多后台进程在运行,因此它们会保存在 LRU (最近最少使用)列表中,以确保包含用户最近查看的 Activity 的进程最后一个被终止。

5).空进程

A process that doesn't hold any activeapplication components. The only reason to keep this kind of process alive isfor caching purposes, to improve startup time the next time a component needsto run in it. The system often kills these processes in order to balanceoverall system resources between process caches and the underlying kernelcaches.

 

 

不含任何活动应用组件的进程。保留这种进程的的唯一目的是用作缓存,以缩短下次在其中运行组件所需的启动时间。为使总体系统资源在进程缓存和底层内核缓存之间保持平衡,系统往往会终止这些进程。

解决问题的方案

由于系统等级的划分有着明确的条件,根据这些条件我们可以制定可行的方案。

通过文档我们可以知道,真正的禁止应用被系统干掉是不可能的,在内存不足的情况下,谁也可能被干掉,而我们要做的就是降低被干掉的概率,并且每个进程都有自己的条件,只有满足了条件就可以成为响应的进程。

根据前台进程的条件,有一种可行方式可以将进程变为前台进程即:运行一个“前台服务”(发一个通知,通知应用正在运行)。

根据服务进程的条件,有一种可行方式可以将进程变为服务进程即:开启服务(这个服务是普通的服务,并没有和前台服务绑定,也没有和前台进程有关系)。

每个应用默认情况下是在一个进程中运行,android系统会尽可能的时间长的保持应用进程运行,但是为了新建进程或者运行更重要的进程,最终需要移除旧的进程来回收内存,而系统根据进程中组件以及这些组件的状态,将进程划为五个等级,并根据等级的重要性来进行资源回收。




1 0
原创粉丝点击