android 进程与线程 - 开发文档翻译 - 进程

劣质翻译系列

由于本人英文能力实在有限,不足之初敬请谅解,希望大家落脚同时能指出不足。

本博客只要没有注明“转”,那么均为原创,转贴请注明链接


android 进程与线程 - 开发文档翻译 - 进程

android 进程与线程 - 开发文档翻译 - 线程


其他系列的翻译

android activity开发文档翻译 - 1 - 基础篇

android activity开发文档翻译 - 2 - 生命周期篇


android task与back stack 开发文档翻译 - 1

android task与back stack 开发文档翻译 - 2

android task与back stack 开发文档翻译 - 3


android Fragment开发文档翻译 - 1

android Fragment开发文档翻译 - 2


本系列并没有对原文100%翻译,也没有100%的贴出原文


导读,下面的内容较重要

1.android进程概述

2.进程生命周期(进程的级别,以及每个级别的生命力)

3.代码级别的一些建议



Processes and Threads

进程和线程


When an application component starts and the application does not have any other components running, the Android system starts 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 and thread (called the "main" thread). 

If an application component starts and there already exists a process for that application (because another component from the application exists), then the component is started within that process and uses the same thread of execution. 

However, you can arrange for different components in your application to run in separate processes, and you can create additional threads for any process.

当一个应用组件启动并且这个应用没有其他组件在运行时,Android为这个应用启动一个新的Linux进程,并在其执行一个单线程。

默认的,同一个应用所有组件运行在同一个进程和线程中(被叫做主线程)

如果一个应用组件启动并且已经存在一个此应用的进程(因为另一个此应用的组件的存在),那么这个组件在此进程内启动并且使用同一个线程来执行

然而你可以让你应用中的不同组件运行在不同的进程中,并且你可以为任何进程建立另外的线程。



This document discusses how processes and threads work in an Android application.

这个文档讨论进程与线程在一个Android应用中如何工作



Processes

进程


By default, all components of the same application run in the same process and most applications should not change this. 

However, if you find that you need to control which process a certain component belongs to, you can do so in the manifest file.

默认的,同一个应用中所有的组件运行在同一个进程中,并且大多数应用不应该改变这种行为。

然而,如果你发现你需要控制某个组件所在的进程,你可以在manifest文件中设置。



The manifest entry for each type of component element—<activity>, <service>, <receiver>, and <provider>—supports an android:process attribute that can specify a process in which that component should run. 

You can set this attribute so that each component runs in its own process or so that some components share a process while others do not. 

You can also set android:process so that components of different applications run in the same process—provided that the applications share the same Linux user ID and are signed with the same certificates.

每一个组件元素<activity>, <service>, <receiver>,和<provider>在manifest的入口,支持android:process属性可以指定组件应该运行在哪一个进程

你可以设置这个属性,这样每一个组件就能运行在它自己的进程或者同一些组件共享同一个进程而其他组件则不会。

你也可以设置android:process来让不同应用中的组件运行在同一个进程中 - 由共享同一个Linux user ID并且具有相同的签名的应用提供。



The <application> element also supports an android:process attribute, to set a default value that applies to all components.

<application>元素也支持android:process属性,用来设置所有组件使用的默认值。



Android might decide to shut down a process at some point, when memory is low and required by other processes that are more immediately serving the user. 

Application components running in the process that's killed are consequently destroyed. 

A process is started again for those components when there's again work for them to do.

当内存低并且其他更紧密服务用户的进程需要内存时,Android也许在一些时刻决定关闭一个进程。

应用组件运行中的进程因此被杀死。

那些组件所在的进程当有工作要做的时候会再次启动。




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

For example, it more readily shuts down a process hosting activities that are no longer visible on screen, compared to a process hosting visible activities. 

The decision whether to terminate a process, therefore, depends on the state of the components running in that process. 

The rules used to decide which processes to terminate is discussed below.

当决定那个进程要被杀死的时候,Android系统权衡他们对用户的重要性。

例如,与可见的activity的宿主进程相比,更容易关闭一个不再在屏幕上可见的activity的宿主进程。

是否终止一个进程的决定,取决于运行于那个进程的组件状态。

下面讨论决定终止进程的规则



Process lifecycle

进程生命周期

The Android system tries to maintain an application process for as long as possible, but eventually needs to remove old processes to reclaim memory for new or more important processes. 

To determine which processes to keep and which to kill, the system places each process into an "importance hierarchy" based on the components running in the process and the state of those components. 

Processes with the lowest importance are eliminated first, then those with the next lowest importance, and so on, as necessary to recover system resources.

Android系统尝试尽可能长的保持一个应用进程,但是最终需要为更重要的进程移除旧的进程来回收内存。

决定哪一个进程保留哪个杀死,系统把每一个进程放到一个"重要层级"中,基于进程中运行的组件和这些组件的状态。

重要性级别最低的最先被清除,然后是与其级别相邻的……如果有必要恢复系统资源的话。



There are five levels in the importance hierarchy. 

The following list presents the different types of processes in order of importance (the first process is most important and is killed last):

"重要层级"有5个级别。

下面的列表呈现出按重要性排列的不同类型的进程(第一个进程是最重要的,最后被杀死)


1.Foreground process

前台进程(Foreground翻译成前景更合适?)

A process that is required for what the user is currently doing. 

A process is considered to be in the foreground if any of the following conditions are true:

用户当前正在使用的进程。

如果下面任何一个条件成立,那么这个进程则被认为是前台进程。


1.It hosts an Activity that the user is interacting with (the Activity's onResume() method has been called).

2.It hosts a Service that's bound to the activity that the user is interacting with.

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

4.It hosts a Service that's executing one of its lifecycle callbacks (onCreate(), onStart(), or onDestroy()).

5.It hosts a BroadcastReceiver that's executing its onReceive() method.

1.它是一个用户正在与之交互的(activity的onResume()已经被调用)activity的宿主进程。

2.它是一个被绑定到用户正与之交互的activity的service的宿主进程

3.它是一个运行在“前台”的service宿主进程(service的startForeground()已经被调用)

4.它是一个正在执行它自己的生命周期回调函数(onCreate(), onStart(),或者onDestroy())的service的宿主进程

5.它是一个正在执行它自己的onReceive()方法的BroadcastReceiver



Generally, only a few foreground processes exist at any given time. 

They are killed only as a last resort—if memory is so low that they cannot all continue to run. 

Generally, at that point, the device has reached a memory paging state, so killing some foreground processes is required to keep the user interface responsive.

总的来说,只有一小部分前台进程在确定的时间退出

如果内存低的使他们都无法继续运行,他们才会作为最后的手段被杀死。

总的来说,在那个时刻,设备达到了内存分页状态,所以保持UI相应需要杀死一些前台进程。

(memory paging state这是什么,完全不理解)


2.Visible process

可见进程

A process that doesn't have any foreground components, but still can affect what the user sees on screen. 

A process is considered to be visible if either of the following conditions are true:

进程没有任何前台组件,但是仍然能影响用户在屏幕上所见的内容。

如果下面任意一个条件成立,那么进程就被认为是可见的


1.It hosts an Activity that is not in the foreground, but is still visible to the user (its onPause() method has been called). 

This might occur, for example, if the foreground activity started a dialog, which allows the previous activity to be seen behind it.

2.It hosts a Service that's bound to a visible (or foreground) activity.

1.它是一个不在前台,但是仍然对用户可见(activity的onPause()已经被调用)的activity的宿主进程。

这是可能发生的,比如,如果这个前台activity打开了一个允许前面的activity在它后面显示的dialog。

2.它是一个绑定到一个可见(或者前台)activity的service的宿主进程。


A visible process is considered extremely important and will not be killed unless doing so is required to keep all foreground processes running.

一个可见进程只有在保持所有前台进程运行的时候才会被认为是很重要的并且不会被杀死。



3.Service process

service进程

A process that is running a service that has been started with the startService() method and does not fall into either of the two higher categories. 

Although service processes are not directly tied to anything the user sees, they are generally doing things that the user cares about (such as playing music in the background or downloading data on the network), so the system keeps them running unless there's not enough memory to retain them along with all foreground and visible processes.

通过startService()方法启动的一个运行service的进程并且不会变成上面两个更高级别的进程。

虽然service进程没有直接绑定到任何用户可见的事物上,但是他们他们做的是基本上都是用户锁关系的(比如在后台播放音乐,或者从网络上下载数据),所以除非在没有足够的内存保持他们与所以前台进程和可见进程的运行,不然系统会保持他们的运行



4.Background process

后台进程

A process holding an activity that's not currently visible to the user (the activity's onStop() method has been called). 

These processes have no direct impact on the user experience, and the system can kill them at any time to reclaim memory for a foreground, visible, or service process. 

Usually there are many background processes running, so they are kept in an LRU (least recently used) list to ensure that the process with the activity that was most recently seen by the user is the last to be killed. 

If an activity implements its lifecycle methods correctly, and saves its current state, killing its process will not have a visible effect on the user experience, because when the user navigates back to the activity, the activity restores all of its visible state. 

See the Activities document for information about saving and restoring state.

一个持有一个现在对用户不可见的activity的进程(activity的onStop()方法已经被调用)

这些进程没有直接影响到用户体验,系统能够在任何时间为了前台进程,可见进程或者service进程回收内存而杀死他们。

通常会有许多后台进程在运行,所以他们被保存在一个LRU列表中来保证带有最近对用户可见的activity的进程最后才被杀死

如果activity正确的实现它的生命周期函数,并且保存了当前状态,杀死它的进程在用户体验上来说则不会有可见的影响,因为当用户导航会这个activity,它会恢复所有它可见的状态。

保存和恢复activity的状态的更多信息参看Activity文档



5.Empty process

空进程

A process that doesn't hold any active application components. 

The only reason to keep this kind of process alive is for caching purposes, to improve startup time the next time a component needs to run in it. 

The system often kills these processes in order to balance overall system resources between process caches and the underlying kernel caches.

进程没有持有任何活动的应用组件。

保留这种进程的唯一原因是缓存目标,来改进组件下次需要运行在其中的启动时间。

系统经常杀死这些进程来使系统资源在进程缓存与底层内核缓存保持平衡。



Android ranks a process at the highest level it can, based upon the importance of the components currently active in the process. 

For example, if a process hosts a service and a visible activity, the process is ranked as a visible process, not a service process.

Android把进程归为其可能成为的最高的级别,基于进程中组件当前的活动的重要性。

例如,如果一个进程是一个service和一个可见activity的宿主进程,进程被归为可见进程而不是一个service进程。



In addition, a process's ranking might be increased because other processes are dependent on it—a process that is serving another process can never be ranked lower than the process it is serving. 

For example, if a content provider in process A is serving a client in process B, or if a service in process A is bound to a component in process B, process A is always considered at least as important as process B.

另外,一个进程的级别可能会增长,因为其他进程对它的依赖 - 服务于其他进程的进程级别不可能低于它服务的进程。

比如,如果进程A中的一个content provider正在服务于进程B中的客户端,或者如果进程A中的一个service绑定到进程B中的一个组件上,进程A总是被认为至少也是与进程B一样重要的。



Because a process running a service is ranked higher than a process with background activities, an activity that initiates a long-running operation might do well to start a service for that operation, rather than simply create a worker thread—particularly if the operation will likely outlast the activity. 

For example, an activity that's uploading a picture to a web site should start a service to perform the upload so that the upload can continue in the background even if the user leaves the activity. 

Using a service guarantees that the operation will have at least "service process" priority, regardless of what happens to the activity. 

This is the same reason that broadcast receivers should employ services rather than simply put time-consuming operations in a thread.

因为运行service的进程的级别会比含有后台activity的级别高,长时间运行的初始化操作的activity,与其简单的建立一个工作线程,不如启动一个service来执行这些操作也许比较好,特别是如果这个操作将比activity寿命还长。

例如,正在上传一张图片到web站点的activity应该启动一个service来执行这个上传操作,这样上传操作就可以在后台继续运行,即使是用户离开了activity。

使用service能保证操作至少有“service进程”的优先级,而不用估计activity发生了什么

这也是broadcast receiver应该使用service而不是简单的把耗时操作放到一个线程中的想用理由。




原文地址如下,英文水平实在有限,希望拍砖同时能给予指正。

http://developer.android.com/guide/components/processes-and-threads.html



转贴请保留以下链接

本人blog地址

http://su1216.iteye.com/

http://blog.csdn.net/su1216/

你可能感兴趣的:(android,service,user,application,文档,Components)