«

Services 翻译第一集

时间:2024-3-2 18:03     作者:韩俊     分类: Android


原文地址:http://blog.csdn.net/wcs542882916

android官方原文地址http://developer.android.com/guide/components/services.html

Services

IN THIS DOCUMENT

The Basics

Declaring a service
in the manifest
Creating a Started
Service

Extending
the IntentService classExtending the
Service classStarting a serviceStopping a service
Creating
a Bound ServiceSending Notifications
to the UserRunning a Service in
the ForegroundManaging the Lifecycle
of a Service

Implementing
the lifecycle callbacks

KEY CLASSES

ServiceIntentService

SAMPLES

ServiceStartArgumentsLocalService

SEE ALSO

Bound 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是一个应用程序组件,它能够在后台执行长时间运行的操作,不提供用户接口。其它的应用程序组件能启动一个service,甚至在用户换切换到其它应用之后这个service仍然继续在后台执行。此外,一个组件能够绑定到一个service来和这个service进行交互,甚至执行进程间通信(IPC)操作。例如,一个service可能处理网络传输,播放音乐,执行文件I/O操作,或者与内容提供者(content provider)进行交互,都可以在后台完成。

A service can essentially take two forms:

一个service从本质上说能够采用两种形式:

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.一个service被启动,当一个应用组件(例如一个activity)通过调用startService()方法启动它。一旦启动,一个service能在后台永久的运行,甚至在启动它的那个组件被摧毁掉后。一般,一个被启动的service执行一个单一的操作,不返回任何结果给调用者。例如,service可能下载或上传一个文件到网络上。当操作完成后,这个service应当停止它自己。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.一个service被绑定,当一个应用程序组件通过调用bindService()来绑定它。一个被绑定的service提供一个c/s接口,这个接口允许组件与service进行交互,发送请求,获得结果,甚至通过IPC跨进程这样操作。一个被绑定的service运行的时间周期与绑定它的组件一样。一次可以多个组件绑定这个service,但是当所有的组件都解绑这个service后,该service才会被摧毁。

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.

尽管这个文档分开讨论了这两种类型的services,但是实际上你的service能够同时工作在两种方式下,service能够被启动(永久的运行)也能被绑定。这是一件很容易的事情,就看你是否实现了这两个回调方法: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.

不管你的应用组件是被启动还是被绑定或两者都有,任何应用组件都能够使用这个service(甚至从另一个应用使用这个service),同样的方式,任何组件能使用一个activity-通过Intent启动它。不管怎么样,你能声明一个service是私有的,在manifest文件,阻止其它应用调用。更多内容请看Declaring the service in the manifest.

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运行在拥有它的进程的主线程中--这个service不会创建它自己的线程,也不会运行在一个独立的进程中(除非你指定一个进程)。意思是说,如果你的service将要做任何CPU密集的工作或者阻塞操作(例如播放MP3或网络任务),你应该在service内创建一个新的线程来做那些工作。通过使用独立的线程,你将会减少应用程序无响应错误(ANR)的风险,而且应用的主线程还能够致力于用户与你activaty的交互。

标签: android

热门推荐