您的位置:首页 > 移动开发 > Android开发

android service 英文文档解析

2015-01-18 00:00 295 查看
摘要: 自己研究了android的英文文档那个 并做了注释 给大家分享一下

android 官方文档可以看出 service是运行在主线程中的

Note that services, like other application objects, run in the main thread of their hosting process.

A Service is not a separate process. The Service object itself does not imply it is running in its own process; unless otherwise specified, it runs in the same process as the application it is part of.

A Service is not a thread. It is not a means itself to do work off of the main thread (to avoid Application Not Responding errors).

Started

A service is "started" when an application component (such as an activity) starts it by calling
startService()
. Once started, a service can run in the background indefinitely(无限的), even if the component that started it is destroyed. Usually, a started service performs a single operation and does not return a result to the caller. For example, it might download or upload a file over the network. When the operation is done, the service should stop itself.

Bound

A service is "bound" when an application component binds to it by calling
bindService()
. A bound service offers a client-server interface that allows components to interact with the service, send requests, get results, and even do so across processes with interprocess commu
7fe0
nication (IPC). A bound service runs only as long as another application component is bound to it. Multiple components can bind to the service at once, but when all of them unbind, the service is destroyed.

Although this documentation generally discusses these two types of services separately, your service can work both ways—it can be started (to run indefinitely) and also allow binding(可以同时被两种方法开启). It's simply a matter of whether you implement a couple callback methods:
onStartCommand()

to allow components to start it and
onBind()

to allow binding.
Caution: A service runs in the main thread of its hosting process—the service does not create its own thread and does not run in a separate process (unless you specify otherwise). This means that, if your service is going to do any CPU intensive work or blocking operations (such as MP3 playback or networking), you should create a new thread within the service to do that work. By using a separate thread, you will reduce the risk of Application Not Responding (ANR) errors and the application's main thread can remain dedicated to user interaction with your activities.

service的销毁
If a component starts the service by calling
startService()
(which results in a call to
onStartCommand()
), then the service remains running until it stops itself with
stopSelf()
or another component stops it by calling
stopService()
.(另一个组件调用stopService()也可以停止service)If a component calls
bindService()
to create the service (and
onStartCommand()
is not called), then the service runs only as long as the component is bound to it. Once the service is unbound from all clients, the system destroys it.
销毁时的优先级:The Android system will force-stop a service only when memory is low and it must recover system resources for the activity that has user focus. If the service is bound to an activity that has user focus, then it's less likely to be killed, and if the service is declared to run in the foreground (discussed later), then it will almost never be killed. Otherwise, if the service was started and is long-running, then the system will lower its position in the list of background tasks over time and the service will become highly susceptible to killing—if your service is started, then you must design it to gracefully handle restarts by the system. If the system kills your service, it restarts it as soon as resources become available again (though this also depends on the value you return from
onStartCommand()
, as discussed later). For more information about when the system might destroy a service, see the Processes and Threading document.(注:只有当内存所剩无几的时候系统才会强制杀死service,而且service具有优先级,如果所属的activity是正在获取用户焦点则最不容易被杀死,相比下,运行的时间越长的service 最容易被杀死,如果是 声明为run in the foreground 永远不会被杀死)Caution:A services runs in the same process as the application in which it is declared and in the main thread of that application,(service在应用的同一进程,并且运行在主线程中) by default. So, if your service performs intensive or blocking operations while the user interacts with an activity from the same application, the service will slow down activity performance. To avoid impacting application performance, you should start a new thread inside the service.
Creating a Started ServiceExtending the IntentService classBecause most started services don't need to handle multiple requests simultaneously (which can actually be a dangerous multi-threading scenario), it's probably best if you implement your service using the
IntentService
class.(并发性不强的情况下使用,每次只执行一个线程,当一个线程执行完成在执行下一个线程)

IntentService


This is a subclass of
Service
that uses a worker thread to handle all start requests, one at a time. (每发一个请求就有一个线程来处理)This is the best option if you don't require that your service handle multiple requests simultaneously. All you need to do is implement
onHandleIntent()
, which receives the intent for each start request so you can do the background work.

Notice that the
onStartCommand()

method must return an integer. The integer is a value that describes how the system should continue the service in the event that the system kills it (as discussed above, the default implementation for
IntentService

handles this for you, though you are able to modify it). The return value from
onStartCommand()

must be one of the following constants:(
onStartCommand()
的返回值决定了系统 如何杀死service)

Stopping a service

A started service must manage its own lifecycle. That is, the system does not stop or destroy the service unless it must recover system memory and the service continues to run after
onStartCommand()
returns. So, the service must stop itself by calling
stopSelf()
or another component can stop it by calling
stopService()
.Caution: It's important that your application stops its services when it's done working, to avoid wasting system resources and consuming battery power. If necessary, other components can stop the service by calling
stopService()
. Even if you enable binding for the service, you must always stop the service yourself if it ever received a call to
onStartCommand()
.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息