@linux1s1s 2017-01-22T08:16:44.000000Z 字数 4060 阅读 1752

AndroidComponent 2015-06

### Android Process

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

#### A foreground process

A foreground process is one that is required for what the user is currently doing. Various application components can cause its containing process to be considered foreground in different ways. A process is considered to be in the foreground if any of the following conditions hold:

• It is running an Activity at the top of the screen that the user is interacting with (its onResume() method has been called).
• It has a Service that is currently executing code in one of its callbacks (Service.onCreate(),Service.onStart(), or Service.onDestroy()).

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.

#### A visible process

A visible process is one holding an Activity that is visible to the user on-screen but not in the foreground (itsonPause() method has been called). This may occur, for example, if the foreground Activity is displayed as a dialog 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.

#### A service process

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.

#### A background process

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 life-cycle 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.

#### An empty process

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.

• 前台进程
• Activity在生命周期内，并且onResume()执行过，此时Activity获得焦点。
• Service.onCreate(),Service.onStart(), or Service.onDestroy()任何一个方法被执行。
• 归结为一句话就是 Applicaton的三大组件在运行中，这个时候就是前台进程。
• 可见进程
• 当Activity在生命周期中，并且onPause执行过，此时Activity失去焦点。
• 服务进程
• 当Service startService()以后，
• 后台进程
• 当Activity在生命周期中，并且onStop执行过，此时Activity不可见。
• 空进程
• 该进程木有Application的任何组件。

### 结论

• A foreground process 当该Acticity是活动的，获得焦点
• A visible process 当该Activity是活动的，但是被遮挡，失去焦点
• A background process 当该Activity不是活动的，被完全盖住

• 此时在 BroadcastOnreceive() 方法中开启一个子线程去做异步操作是可行的。
• 对于比较特殊的项目来说，没有任何Activity存在，也没有任何Service存在，那么在执行到OnReceive()，此时Apk进程是一个A foreground process，而当执行结束以后，此时Apk进程很可能是An empty process，所以系统很容易结束掉这个Apk进程，所以在OnReceive()中启用一个子线程是不安全的，如果此时在OnReceive()中启用Service，那么起码在OnReceive()中执行结束以后，Apk进程还可以处于A service process，所以不可能被系统无端Kill掉

• 私有
• 公开
• 删除