Services 安卓服务

原:

A Service is an application component that can perform long-running operations in the background and does not provide a user interface. Another application component can start a service and it will continue to run in the background even if the user switches to another application. Additionally, a component can bind to a service to interact with it and even perform interprocess communication (IPC). For example, a service might handle network transactions, play music, perform file I/O, or interact with a content provider, all from the background.

译:
Service是安卓应用程序的一个组件,它可以在后台处理长时间运行的事务,但是并不提供用户界面。应用程序的其他组件可以启动一个服务,甚至当用户切换到其他应用程序的时候,该服务仍然可以在后台运行。另外,一个组件可以绑定到一个服务并和该服务进行交互,可以使用IPC进行进程间通信。例如,一个服务可以处理网络传输任务,播放音乐,进行文件的IO操作,或者和content provider进行交互,一切都是在后台进行。

A service can essentially take two forms:

一个服务基本上有两种形式:

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.

当应用程序的一个组件调用startService()方法启动一个服务的时候,这个服务就“开始运行”了。一旦启动之后,该服务便会在后台无限期地运行下去,甚至在启动该服务的组件被销毁之后仍然可以运行。通常情况下,一个服务只进行单一的任务并且不会向调用者返回结果。例如,它可以从网络上下载或者上传一个文件。当任务结束之后,服务应该自动结束。

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 communication (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.

当应用程序的组件调用bindService()将自己绑定到一个服务的时候,该服务便被“绑定”了。一个绑定的服务会提供一个客户端-服务器接口,允许组件和服务进行交互,发送请求,获取结果,甚至可以通过使用IPC跨进程进行上述操作。只有当应用程序的组件绑定了服务之后,该服务才会运行。多个组件可以同时绑定到一个服务上,只有当所有的组件都接触服务绑定之后,服务才会销毁。

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.

译:
虽然这篇文档大体上分别讨论两种类型的服务,你的服务可以同时使用——服务可以被启动(无限期的运行),也可以允许绑定。情况仅仅在于你什么时候实现一对回调方法:onStartCommand()方法允许组件启动服务,onBind()方法允许绑定服务。

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

无论你的应用程序是否启动服务,绑定服务,或者同时启动、绑定,任何应用程序的组件都可以使用服务(甚至可以从其他的应用程序中启动、绑定服务),同样,任何组件可以使用activity——通过一个Intent来启动。你可以在布局文件中将服务声明为私有的,就可以阻止其他应用程序访问该组件。这部分内容将在以下章节有更过的讨论:在布局文件中声明服务

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.

注意:一个服务在其宿主进程的主线程中运行——服务并不会创建属于它自己的线程,也不会在另外的进程中运行(除非你额外指定)。这意味着,如果你的服务将要运行任何消耗CPU的工作或者阻塞操作(例如MP3播放或者网络操作),你应该在服务内部创建一个新的线程来做这项工作。通过使用一个额外的线程,你将会降低应用程序不响应(ANR)错误的风险,并且应用程序的主线程可以专注于和你的activities进行用户交互。

The Basics


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

为了创建一个服务,你必须要创建一个Service 的子类(或者是Service 已经存在的子类的子类)。在你的实现代码中,你需要复写一些回调方法,这些回调方法会处理Service 生命周期内的关键事务,并且提供一套用于组件绑定服务的机制。那些需要你复写的非常重要的回调方法如下:

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 the background indefinitely. If you implement this, it is your responsibility to stop the service when its work is done, by calling stopSelf() or stopService(). (If you only want to provide binding, you don’t need to implement this method.)

当一个的组件,例如activity,请求启动服务的时候,

onBind()
The system calls this method when another component wants to bind with the service (such as to perform RPC), by calling bindService(). In your implementation of this method, you must provide an interface that clients use to communicate with the service, by returning an IBinder. You must always implement 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 setup procedures (before it calls either onStartCommand() or onBind()). If the service is already running, this method is not called.

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, registered listeners, receivers, etc. This is the last call the service receives.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值