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

Android Service官方文档的介绍

2012-03-03 15:59 393 查看
A
Service
is an application component that can performlong-running operations in the background and does not provide a user interface. Anotherapplication component can start a service and it will continue to run in the background even
if theuser switches to another application. Additionally, a component can bind to a service tointeract with it and even perform interprocess communication (IPC). For example, a service mighthandle network transactions, play music, perform file I/O, or interact
with a content provider, allfrom the background.

A service can essentially take two forms:

StartedA service is "started" when an application component (such as an activity) starts it bycalling
startService()
. Once started, a servicecan 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, theservice should stop itself.BoundA service is "bound" when an application component binds to it by calling
bindService()
. A bound service offers a client-serverinterface that allows components to interact with the service, send requests, get results, and evendo so across processes with interprocess communication (IPC). A bound service runs only as
long asanother 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, yourservice 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.

Regardless of whether your application is started, bound, or both, any application componentcan use the service (even from a separate application), in the same way that any component can usean activity—by starting it with an
Intent
. However,
you can declarethe service as private, in the manifest file, and block access from other applications. This isdiscussed more in the section aboutDeclaring the service in themanifest.

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


The Basics

To create a service, you must create a subclass of
Service
(or oneof its existing subclasses). In your implementation, you need to override some callback methods thathandle key aspects of the service lifecycle and provide a mechanism
for components to bind tothe service, if appropriate. The most important callback methods you should override are:

onStartCommand()
The system calls this method when another component, such as an activity,requests that the service be started, by calling
startService()
. Once this method executes, the service is started and can run in thebackground indefinitely. If
you implement this, it is your responsibility to stop the service whenits work is done, by calling
stopSelf()
or
stopService()
. (If you only want to provide binding, you don'tneed to implement this method.)
onBind()
The system calls this method when another component wants to bind with theservice (such as to perform RPC), by calling
bindService()
. In your implementation of this method, you must provide an interface that clientsuse to communicate
with the service, by returning an
IBinder
. You must alwaysimplement this method, but if you don't want to allow binding, then you should return null.
onCreate()
The system calls this method when the service is first created, to perform one-time setupprocedures (before it calls either
onStartCommand()
or
onBind()
). If the service is already running, this method is notcalled.
onDestroy()
The system calls this method when the service is no longer used and is being destroyed.Your service should implement this to clean up any resources such as threads, registeredlisteners, receivers, etc. This is the last call the service receives.
If a component starts the service by calling
startService()
(which results in a call to
onStartCommand()
), then the serviceremains running until it stops itself with
stopSelf()
or anothercomponent
stops it by calling
stopService()
.

If a component calls
bindService()
to create the service (and
onStartCommand()
isnot called), then the service runsonly as long as the component is bound to it. Once the service is unbound from all clients,
thesystem destroys it.

The Android system will force-stop a service only when memory is low and it must recover systemresources for the activity that has user focus. If the service is bound to an activity that has userfocus, 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 positionin the list of background tasks over time and the service will
become highly susceptible tokilling—if your service is started, then you must design it to gracefully handle restartsby the system. If the system kills your service, it restarts it as soon as resources becomeavailable again (though this also depends on the
value you return from
onStartCommand()
, as discussed later). For more informationabout when the system might destroy a service, see theProcesses and Threadingdocument.

In the following sections, you'll see how you can create each type of service and how to useit from other application components.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: