API翻译 --- Bound Services

目录:

1.The Basics

最基本的

2.Declaring a service in the manifest

在服务清单中配置

3.Creating a Started Service Extending the IntentService class

创建一个服务扩展类

4.Extending the Service class

继承Serrvice类

5.Starting a service

开始服务

6.Stopping a service

结束服务

7.Creating a Bound Service

绑定服务

8.Sending Notifications to the User

向用户发送通知

9.Running a Service in the Foreground

在前台运行服务

10.Managing the Lifecycle of a Service

管理服务的生命周期

11.Implementing the lifecycle callbacks

实现生命周期回调

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.

服务是一个应用程序组件,可以在后台执行长时间运行的操作,不提供用户界面。另一个应用程序组件可以启动一个服务,它将继续在后台运行,即使用户切换到另一个应用程序。此外,一个组件可以绑定到一个服务与它交互,甚至执行进程间通信(IPC)。例如,一个服务可能处理网络交易,播放音乐,执行文件I / O,或与一个内容提供者交互,所有的背景。

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.

无论您的应用程序启动时,绑定,或者两者兼有,任何应用程序组件可以使用服务(甚至从一个单独的应用程序),以同样的方式,任何组件都可以使用一个活动开始与一个意图。然而,您可以声明与私人服务,在清单文件,阻止访问其他应用程序。这一节讨论更多的是关于声明的服务清单。

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)错误和应用程序的主线程可以仍然致力于用户交互活动。

The Basics  最基本的

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.)

onStartCommand()

系统调用这个方法当另一个组件,比如一个活动,要求服务启动,通过调用由startService()。此方法执行后,服务启动,可以在后台运行下去。如果你实现这个,是你的责任停止服务工作完成后,通过调用stopSelf()或stopService()。(如果你只是想提供绑定,您不需要实现此方法)。

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.

onBind()

系统调用该方法当另一个组件想绑定的服务(如执行RPC),通过调用bindService()。在你实现这种方法,您必须提供一个接口,客户使用与服务进行通信,通过返回一个内部。你必须实现这个方法,但是如果你不希望允许绑定,那么您应该返回null。

onCreate()

The system calls this method when the service is first created, to perform one-time setup procedures (before it calls either onStartCommand() oronBind()). If the service is already running, this method is not called.

onCreate()

系统调用此方法在创建服务第一,执行一次性安装程序(之前调用要么onStartCommand()oronBind())。如果服务已经运行,调用这个方法并不是。

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.

onDestroy()

系统调用此方法不再使用服务时,被摧毁了。您的服务应该实现这个清理任何资源,如线程注册侦听器,接收器,等等。这是最后一个调用服务。

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

如果一个组件启动服务通过调用由startService()(它导致调用onStartCommand()),然后服务仍然运行,直到它停止本身stopSelf()或停止它通过调用另一个组件stopService()。

If a component callsbindService() 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.

如果一个组件callsbindService()来创建服务(onStartCommand()不是),然后服务只要运行组件绑定到它。一旦释放从所有客户服务,系统破坏它。

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 Threadingdocument.

Android系统将force-stop服务只有当内存低,必须恢复系统资源的活动用户的焦点。如果服务被绑定到一个活动用户的焦点,那就不太可能被杀,如果声明的服务是运行在前台(稍后讨论),那么它将几乎从未被杀死。否则,如果服务是启动和长期,则系统会降低它在后台任务列表位置随着时间的推移和服务将成为高度容易killing-if启动你的服务,那么你必须设计优雅地处理系统重新启动。如果系统杀死你的服务,再次重新启动它一旦资源可用(尽管这也取决于你从onStartCommand返回的值(),稍后讨论)。更多信息时,系统可能会摧毁一个服务,流程和Threadingdocument。

In the following sections, you‘ll see how you can create each type of service and how to use it from other application components.

在下面几节中,您将看到如何创建每个类型的服务和如何使用它从其他应用程序组件。

hould you use a service or a thread?

A service is simply a component that can run in the background even when the user is not interacting with your application. Thus, you should create a service only if that is what you need.

你应该使用一个服务或一个线程?

服务是一个简单的组件,可以在后台运行,即使用户与应用程序交互。因此,您应该只创建一个服务如果这是你所需要的东西。

A service is simply a component that can run in the background even when the user is not interacting with your application. Thus, you should create a service only if that is what you need.

If you need to perform work outside your main thread, but only while the user is interacting with your application, then you should probably instead create a new thread and not a service. For example, if you want to play some music, but only while your activity is running, you might create a thread in onCreate(), start running it in onStart(), then stop it in onStop(). Also consider using AsyncTask or HandlerThread, instead of the traditional Thread class. See the Processes?and?Threading document for more information about threads.

Remember that if you do use a service, it still runs in your application‘s main thread by default, so you should still create a new thread within the service if it performs intensive or blocking operations.

服务是一个简单的组件,可以在后台运行,即使用户与应用程序交互。因此,您应该只创建一个服务如果这是你所需要的东西。

如果您需要执行工作外你的主线程,但只有在用户与应用程序交互,那么你应该而不是创建一个新的线程,而不是服务。例如,如果你想播放一些音乐,但只有当你的活动正在运行,您可以创建一个线程在onCreate(),在onStart()开始运行它,然后停止在onStop()。也可以考虑使用AsyncTask或HandlerThread,而不是传统的线程类。有关更多信息,请参见进程和线程文档关于线程。

记住,如果你使用一个服务,它仍然运行在应用程序的主线程在默认情况下,所以你应该还是创建一个新的线程内的服务如果它执行密集或阻塞操作。

Declaring a service in the manifest    //在manifest中配置

Like activities (and other components), you must declare all services in your application‘s manifest file.

像activity(和其他组件),你必须在您的应用程序的清单文件声明所有服务。

To declare your service, add a <service> element as a child of the <application> element. For example:

声明服务:在 <application>标签下添加一个<service>标签,如下:

复制代码:

<manifest ... >

...

<application ... >

<service android:name=".ExampleService" />

...

</application>

</manifest>

There are other attributes you can include in the <service> element to define properties such as permissions required to start the service and the process in which the service should run. The android:name attribute is the only required attribute—it specifies the class name of the service. Once you publish your application, you should not change this name, because if you do, you might break some functionality where explicit intents are used to reference your service (read the blog post, Things That Cannot Change).

还有其他属性可以包含在<service>标签中定义,如需要启动服务的权限和服务运行的过程。android:name属性是它需要指定服务的唯一类名。一旦应用程序发布,你不能改变这个名字,因为如果这样做,你可能会破坏一些功能,使用显式意图来引用您的服务(阅读博客,关于不能改变的事情)。

See the <service> element reference for more information about declaring your service in the manifest.

在服务清单中查看关于<service> 标签的更多信息

Just like an activity, a service can define intent filters that allow other components to invoke the service using implicit intents. By declaring intent filters, components from any application installed on the user‘s device can potentially start your service if your service declares an intent filter that matches the intent another application passes to startService().

就像一个活动,一个服务可以定义一个intent过滤器,允许其他组件调用该服务隐式意图。通过声明意图过滤器,从任何应用程序组件安装在用户的设备可以开始你的服务如果你声明了一个意图过滤器意图相匹配的另一个应用程序通过由startService()。

If you plan on using your service only locally (other applications do not use it), then you don‘t need to (and should not) supply any intent filters. Without any intent filters, you must start the service using an intent that explicitly names the service class. More information about starting a service is discussed below.

如果你打算只在本地使用你的服务(其他应用程序不使用),那么你不需要(也不应该)提供任何意图过滤器。没有意图过滤器,您必须使用一个显示的意图来启动该服务,明确服务类名。下面将讨论关于启动服务的更多信息。

Additionally, you can ensure that your service is private to your application only if you include the android:exported attribute and set it to "false". This is effective even if your service supplies intent filters.

另外,如果你设置 android:exported属性是“false”的话你必须保证你的服务是受保护的应用程序。即使你的服务有意图过滤器,这也是有效的。

For more information about creating intent filters for your service, see the Intents and Intent Filters document.

要了解更多关于服务创建过滤器的信息,参考意图和意图过滤器文件。

Creating a Started Service     //创建一个启动服务

A started service is one that another component starts by calling startService(), resulting in a call to the service‘s onStartCommand() method.

一个开始了的服务是由另一个组件通过调用startService()方法开启的,然后直接调用service的onStartCommand()方法。

When a service is started, it has a lifecycle that‘s independent of the component that started it and the service can run in the background indefinitely, even if the component that started it is destroyed. As such, the service should stop itself when its job is done by calling stopSelf(), or another component can stop it by calling stopService().

当启动一个服务,组件会启动一个独立的声明周期并且service会一直在后台运行,即使启动它的组件被销毁。因此服务是通过调用stopSelf()来停止的,或者是另一个组件通过调用stopService()来停止它。

An application component such as an activity can start the service by calling startService() and passing an Intent that specifies the service and includes any data for the service to use. The service receives this Intent in the onStartCommand() method.

应用程序组件,如一个activity可以通过调用startService()方法来启动并且通过意图指定服务所需的所有数据,指定服务的服务,包括任何数据使用。服务在onStartCommand()方法接收到这个意图。

For instance, suppose an activity needs to save some data to an online database. The activity can start a companion service and deliver it the data to save by passing an intent to startService(). The service receives the intent in onStartCommand(), connects to the Internet and performs the database transaction. When the transaction is done, the service stops itself and it is destroyed.

例如,一个activity需要保存数据在一个在线数据库。activity可以开启一个同伴服务并把它保存的数据释放给一个由startService()的意图。服务在onStartCommand()方法中接受意图,连接到互联网和执行数据库操作。当事务完成,服务停止并被摧毁。

Caution: A services runs in the same process as the application in which it is declared and in the main thread of that application, 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.

警告:默认情况下,一个服务运行在声明它的主线程中运行。因此,如果用户在相同的应用程序中执行密集或阻塞操作时,服务将减缓活动性能。为了避免影响应用程序的性能,您应该开启一个新的线程。

Traditionally, there are two classes you can extend to create a started service:

传统上,有两个类可以被继承来创建一个启动服务:

Service

This is the base class for all services. When you extend this class, it‘s important that you create a new thread in which to do all the service‘s work, because the service uses your application‘s main thread, by default, which could slow the performance of any activity your application is running.

这是所有服务的基类。继承这个类时,重要的是,你需要创建一个新的线程来做服务的工作,因为该服务在应用程序的主线程下,默认情况下,这可能会减缓应用程序运行时的性能。

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.

这是一个服务的子类,使用一个工作线程来处理所有开始请求,一次一个。在你不需要你的服务同时处理多个请求的情况下这是最好的选择。你需要做的就是实现onHandleIntent()方法,它为每个请求进行响应,这样你就可以开始做后台工作。

The following sections describe how you can implement your service using either one for these classes.

下面是描述如何使用这些类实现你的服务。

Extending the IntentService class     //继承IntentService类

Because 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类。

The IntentService does the following:

IntentService类做一下几点:

1.Creates a default worker thread that executes all intents delivered to onStartCommand() separate from your application‘s main thread.

创建一个默认的线程,在onStartCommand()将所有的意图传递给独立于应用程序的主线程。

2.Creates a work queue that passes one intent at a time to your onHandleIntent() implementation, so you never have to worry about multi-threading.

创建一个队列,将意图传递给你的onHandleIntent(),所以你永远不必担心多线程。

3.Stops the service after all start requests have been handled, so you never have to call stopSelf().

在请求被处理之后停止服务,所以你不必调用stopSelf()方法。

4.Provides default implementation of onBind() that returns null.

提供的默认实现方法onBind()返回null。

5.Provides a default implementation of onStartCommand() that sends the intent to the work queue and then to your onHandleIntent() implementation.

提供了一个默认实现方法onStartCommand(),将发送意图到工作队列,然后发送到onHandleIntent()。

All this adds up to the fact that all you need to do is implement onHandleIntent() to do the work provided by the client. (Though, you also need to provide a small constructor for the service.)

综上所述,所有您需要在onHandleIntent()需要做的工作都由客户提供。(虽然,您还需要提供服务的小型构造函数)。

Here‘s an example implementation of IntentService:

下面是一个IntentService示例:

复制代码:

public class HelloIntentService extends IntentService {

/**

* A constructor is required, and must call the super IntentService(String)

* constructor with a name for the worker thread.

*/

public HelloIntentService() {

super("HelloIntentService");

}

/**

* The IntentService calls this method from the default worker thread with

* the intent that started the service. When this method returns, IntentService

* stops the service, as appropriate.

*/

@Override

protected void onHandleIntent(Intent intent) {

// Normally we would do some work here, like download a file.

// For our sample, we just sleep for 5 seconds.

long endTime = System.currentTimeMillis() + 5*1000;

while (System.currentTimeMillis() < endTime) {

synchronized (this) {

try {

wait(endTime - System.currentTimeMillis());

} catch (Exception e) {

}

}

}

}

}

That‘s all you need: a constructor and an implementation of onHandleIntent().

这是你所需要的:一个构造函数和实现onHandleIntent()。

If you decide to also override other callback methods, such as onCreate(), onStartCommand(), or onDestroy(), be sure to call the super implementation, so that the IntentService can properly handle the life of the worker thread.

如果你决定也重写其它回调方法,如onCreate(),onStartCommand(),或onDestroy(),一定要调用超级实现类,以便IntentService能妥善处理线程的生命。

For example, onStartCommand() must return the default implementation (which is how the intent gets delivered to onHandleIntent()):

例如,onStartCommand()必须返回默认实现类(意图如何交付给onHandleIntent()):

复制代码:

@Overridepublicint onStartCommand(Intent intent,int flags,int startId){Toast.makeText(this,"service starting",Toast.LENGTH_SHORT).show();returnsuper.onStartCommand(intent,flags,startId);}

Besides onHandleIntent(), the only method from which you don‘t need to call the super class is onBind() (but you only need to implement that if your service allows binding).

除了onHandleIntent(),唯一的方法,你不需要调用超类是onBind()(但你只需要实现,如果您的服务允许绑定)。

In the next section, you‘ll see how the same kind of service is implemented when extending the base Service class, which is a lot more code, but which might be appropriate if you need to handle simultaneous start requests.

在下一节中,您将看到同样的服务是如何实现的扩展基础服务类的时候,这是一个更多的代码,但这可能是合适的,如果你需要处理并发请求开始。

Starting a Service

启动一个服务

You can start a service from an activity or other application component by passing an Intent (specifying the service to start) to startService(). The Android system calls the service‘s onStartCommand() method and passes it the Intent. (You should never call onStartCommand() directly.)

您可以从一个活动或其他应用程序组件启动一个服务通过一个目的(指定服务开始)IntentstartService()方法,。Android系统调用服务的onStartCommand()方法并传递它的意图。(你不应该叫onStartCommand()直接)。

For example, an activity can start the example service in the previous section (HelloSevice) using an explicit intent with startService():

例如,一个活动可以开始一个显式意图使用startService(),例如前一节中的示例服务(HelloSevice):

Intent intent = new Intent(this, HelloService.class);startService(intent);

The startService() method returns immediately and the Android system calls the service‘sonStartCommand() method. If the service is not already running, the system first calls onCreate(), then callsonStartCommand().

立即返回,startService()方法Android系统调用服务sonStartCommand()方法。如果服务没有运行,系统首先调用onCreate(),然后调用callsonStartCommand()方法

If the service does not also provide binding, the intent delivered with startService() is the only mode of communication between the application component and the service. However, if you want the service to send a result back, then the client that starts the service can create a PendingIntent for a broadcast (withgetBroadcast()) and deliver it to the service in the Intent that starts the service. The service can then use the broadcast to deliver a result.

如果服务不提供绑定,交付的意图与startService()是应用程序组件之间唯一的通信模式和服务。然而,如果你希望服务发回一个结果,然后启动服务的客户端,则可以创建PendingIntent广播(withgetBroadcast())和交付的服务启动的目的服务。服务可以使用广播发表的结果

Multiple requests to start the service result in multiple corresponding calls to the service‘s onStartCommand(). However, only one request to stop the service (with stopSelf() or stopService()) is required to stop it.

多个请求启动相应服务调用服务的onStartCommand()会产生多样的结果。然而,只有一个请求停止服务(例如通过stopSelf()或stopService())必须阻止它。

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

开始服务必须管理自己的生命周期。也就是说,系统并不能阻止或破坏服务,除非它必须恢复系统内存和服务onStartCommand后继续运行()返回,服务本身必须停止调用stopSelf()或另一个组件通过调用stopService()去阻止它

Once requested to stop with stopSelf() or stopService(), the system destroys the service as soon as possible.

一旦通过stopSelf()或stopService()去停止请求,系统就会很快的破坏服务。

However, if your service handles multiple requests to onStartCommand() concurrently, then you shouldn‘t stop the service when you‘re done processing a start request, because you might have since received a new start request (stopping at the end of the first request would terminate the second one). To avoid this problem, you can use stopSelf(int) to ensure that your request to stop the service is always based on the most recent start request. That is, when you call stopSelf(int), you pass the ID of the start request (the startId delivered toonStartCommand()) to which your stop request corresponds. Then if the service received a new start request before you were able to call stopSelf(int), then the ID will not match and the service will not stop.

然而,如果你的服务同时处理多个请求onStartCommand(),那么当你处理一个请求开始,你不应该停止服务,因为你可能已经收到了新的开始请求(停在第一个请求终止第二个)。为了避免这个问题,您可以使用stopSelf(int),以确保你的请求停止服务,它总是基于最近的请求开始。也就是说,当你使用stopSelf(int),您就会终止ID请求开始(startId交付toonStartCommand())。

如果服务收到了新的开始请求之前你调用stopSelf(int),ID匹配,服务也将不会停止

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 callingstopService(). Even if you enable binding for the service, you must always stop the service yourself if it ever received a call to onStartCommand().

注意:您的应用程序停止工作时您的应用就会停止活动,这样做是为了避免系统资源浪费和消耗电池,它是非常重要的。如果有必要,可以阻止其他组件的服务callingstopService()。

即使你启用绑定的服务,,如果它调用onStartCommand()方法,那么你必须停止服务。

For more information about the lifecycle of a service, see the section below about Managing?the?Lifecycle?of?a?Service.

关于生命周期服务的更多信息,参见下面一节关于管理服务的生命周期

Creating a Bound Service

创建一个绑定服务

A bound service is one that allows application components to bind to it by calling bindService() in order to create a long-standing connection (and generally does not allow components to start it by callingstartService()).

绑定服务,允许应用程序组件绑定到它通过调用bindService()来创建一个长期的连接(一般不允许组件开始通过callingstartService())。

You should create a bound service when you want to interact with the service from activities and other components in your application or to expose some of your application‘s functionality to other applications, through interprocess communication (IPC).

您应该创建一个绑定服务,当你想与服务交互活动和其他组件在应用程序或向其他应用程序公开您的应用程序的一些功能,通过进程间通信(IPC)。

To create a bound service, you must implement the onBind() callback method to return an IBinder that defines the interface for communication with the service. Other application components can then call bindService() to retrieve the interface and begin calling methods on the service. The service lives only to serve the application component that is bound to it, so when there are no components bound to the service, the system destroys it (you do not need to stop a bound service in the way you must when the service is started throughonStartCommand()).

创建一个绑定服务,您必须实现onBind()回调方法返回一个ibinder定义的接口用来沟通服务。其他应用程序组件可以调用bindService()来检索界面,开始调用服务方法。服务仅仅是寄居在应用程序上来达到绑它的效果,所以当没有组件绑定到服务,系统破坏它(你不需要停止绑定服务的时候必须在启动服务时throughonStartCommand())。

To create a bound service, the first thing you must do is define the interface that specifies how a client can communicate with the service. This interface between the service and a client must be an implementation ofIBinder and is what your service must return from the onBind() callback method. Once the client receives theIBinder, it can begin interacting with the service through that interface.

创建一个绑定服务,你必须做的第一件事是定义接口,指定客户端如何与服务通信。这个接口在服务和客户机之间必须实现ofIBinder()方法,客户端服务必须返回onBind()回调方法,一旦客户端收到theIBinder,它就可以开始通过该接口与服务交互

Multiple clients can bind to the service at once. When a client is done interacting with the service, it callsunbindService() to unbind. Once there are no clients bound to the service, the system destroys the service.

多个客户端可以绑定到服务。当客户端与服务交互,它callsunbindService()来解开。一旦没有客户端绑定到服务,服务系统破坏。

There are multiple ways to implement a bound service and the implementation is more complicated than a started service, so the bound service discussion appears in a separate document about Bound?Services.

有多种方法来实现绑定服务,实现绑定服务比开始服务更复杂,因此,在一个单独的文档绑定服务讨论绑定服务。

Sending Notifications to the User

向用户发送通知

Once running, a service can notify the user of events using Toast?Notifications or Status?Bar?Notifications.

一旦运行,服务可以通知该活动的用户使toast来的到通知或状态栏得到通知。

A toast notification is a message that appears on the surface of the current window for a moment then disappears, while a status bar notification provides an icon in the status bar with a message, which the user can select in order to take an action (such as start an activity).

toast出的消息出现在当前窗口的表面时,过一会儿消失,而提供了一个状态栏通知图标在状态栏信息,用户可以选择采取一个动作(如启动一个活动)。

Usually, a status bar notification is the best technique when some background work has completed (such as a file completed downloading) and the user can now act on it. When the user selects the notification from the expanded view, the notification can start an activity (such as to view the downloaded file).

通常, 在一些背景工作完成(如文件下载完成)或者用户现在进行操作时,一个状态栏通知最好的方法。当用户通过扩展视来选择通知,这个通知可以启动一个活动(如查看下载的文件)。

See the Toast?Notifications or Status?Bar?Notifications developer guides for more information.

领会toast或状态栏通知获得消息指导开发人员获得更多信息

Running a Service in the Foreground

A foreground service is a service that‘s considered to be something the user is actively aware of and thus not a candidate for the system to kill when low on memory. A foreground service must provide a notification for the status bar, which is placed under the "Ongoing" heading, which means that the notification cannot be dismissed unless the service is either stopped or removed from the foreground.

前台服务是一种服务,被认为是用户正在意识到,因此不适合系统杀时内存不足。前台服务必须提供状态栏通知,这是把“正在进行”的标题下,这意味着通知不能被解雇,除非服务被停止或从前台移除。

For example, a music player that plays music from a service should be set to run in the foreground, because the user is explicitly aware of its operation. The notification in the status bar might indicate the current song and allow the user to launch an activity to interact with the music player.

例如,一个音乐播放器,播放音乐,从服务应设置在前台运行,因为用户很关心它的操作。状态栏中的通知可能指示当前的歌曲,并允许用户启动一个活动与音乐播放器交互

To request that your service run in the foreground, call startForeground(). This method takes two parameters: an integer that uniquely identifies the notification and the Notification for the status bar. For example:

要求在前台运行您的服务,电话startforeground()。这种方法需要两个参数:一个唯一标识的通知和状态栏通知整数。例如:

Notification notification = new Notification(R.drawable.icon, getText(R.string.ticker_text),

System.currentTimeMillis());

Intent notificationIntent = new Intent(this, ExampleActivity.class);

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

notification.setLatestEventInfo(this, getText(R.string.notification_title),

getText(R.string.notification_message), pendingIntent);

startForeground(ONGOING_NOTIFICATION, notification);

To remove the service from the foreground, call stopForeground(). This method takes a boolean, indicating whether to remove the status bar notification as well. This method does not stop the service. However, if you stop the service while it‘s still running in the foreground, then the notification is also removed.

从前台移除服务,电话stopforeground()。该方法以一个布尔值,指示是否删除状态栏通知等。此方法不会停止服务。然而,如果你停止服务,而它仍然运行在前台,然后通知也被取消了。

Note: The methods startForeground() and stopForeground() were introduced in Android 2.0 (API Level 5). In order to run your service in the foreground on older versions of the platform, you must use the previoussetForeground() method—see the startForeground() documentation for information about how to provide backward compatibility.

注 意:方法startForeground()和stopForeground()介绍了Android 2.0(API级别5)。为了运行在前台服务的旧版本的平台,您必须使用前面setForeground()method-see startForeground()的文档信息如何提供向后兼容性。

For more information about notifications, see Creating?Status?Bar?Notifications.

关于通知的更多信息,请参阅创建状态栏通知。

Managing the Lifecycle of a Service

管理服务的生命周期

The lifecycle of a service is much simpler than that of an activity. However, it‘s even more important that you pay close attention to how your service is created and destroyed, because a service can run in the background without the user being aware.

服务的生命周期比活动的简单的多。然而,更重要的是,你要密切关注你的服务的创建和销毁,因为服务可以在后台运行,用户不知道。

The service lifecycle—from when it‘s created to when it‘s destroyed—can follow two different paths:

1. A started service

2. 启动服务

The service is created when another component calls startService(). The service then runs indefinitely and must stop itself by calling stopSelf(). Another component can also stop the service by callingstopService(). When the service is stopped, the system destroys it..

服务时创建另一个组件调用 startservice()。然后服务无限期地运行,必须停止本身通过调用 stopself()。另一个组件也可以通过callingstopservice()停止服务。当服务停止,系统销毁它。

1. A bound service

2. 绑定服务

The service is created when another component (a client) calls bindService(). The client then communicates with the service through an IBinder interface. The client can close the connection by callingunbindService(). Multiple clients can bind to the same service and when all of them unbind, the system destroys the service. (The service does not need to stop itself.)

服务时创建另一个组件(客户端)称 bindservice()。客户服务通过一个 IBinder接口进行通信 。客户可以通过callingunbindservice()关闭连接。多个客户端可以绑定到相同的服务时,他们都断开,系统销毁服务。(服务不 不 需要自行停止。)

These two paths are not entirely separate. That is, you can bind to a service that was already started withstartService(). For example, a background music service could be started by calling startService() with an Intent that identifies the music to play. Later, possibly when the user wants to exercise some control over the player or get information about the current song, an activity can bind to the service by calling bindService(). In cases like this, stopService() or stopSelf() does not actually stop the service until all clients unbind.

这两种方法并不是完全分开的。那就是,你可以绑定到一个服务已经开始withstartservice()。例如,一个背景音乐服务可以由一个 意图 确认音乐播放呼叫 startservice() 开始。后来,可能是当用户想练习控制播放器或取得当前歌曲的信息,一个活动可以绑定到服务通过调用 bindservice()。在这样的情况下, stopservice() 或 stopself() 实际上并没有停止服务,直到所有的客户数据。

Implementing the lifecycle callbacks

实施的生命周期回调

Like an activity, a service has lifecycle callback methods that you can implement to monitor changes in the service‘s state and perform work at the appropriate times. The following skeleton service demonstrates each of the lifecycle methods:

活动一样,服务生命周期回调方法,你可以实现监控服务状态的改变,并在适当的时候进行工作。下面的每个骨架服务演示了生命周期方法:

public class ExampleService extends Service {

int mStartMode;       // indicates how to behave if the service is killed

IBinder mBinder;      // interface for clients that bind

boolean mAllowRebind; // indicates whether onRebind should be used

@Override

public void onCreate() {

// The service is being created

}

@Override

public int onStartCommand(Intent intent, int flags, int startId) {

// The service is starting, due to a call to startService()

return mStartMode;

}

@Override

public IBinder onBind(Intent intent) {

// A client is binding to the service with bindService()

return mBinder;

}

@Override

public boolean onUnbind(Intent intent) {

// All clients have unbound with unbindService()

return mAllowRebind;

}

@Override

public void onRebind(Intent intent) {

// A client is binding to the service with bindService(),

// after onUnbind() has already been called

}

@Override

public void onDestroy() {

// The service is no longer used and is being destroyed

}}

Note: Unlike the activity lifecycle callback methods, you are not required to call the superclass implementation of these callback methods.

注: 与活动的生命周期回调方法,你 不 需要调用的回调方法的父类的实现。

Figure 2. The service lifecycle. The diagram on the left shows the lifecycle when the service is created withstartService() and the diagram on the right shows the lifecycle when the service is created with bindService().

图2。 服务生命周期。左边的图显示了生命周期服务时创建的withstartservice() 和右边的图显示了生命周期当服务与 bindservice()创建。

By implementing these methods, you can monitor two nested loops of the service‘s lifecycle:

通过实现这些方法,你可以监控两个嵌套的循环服务的生命周期:

1. The entire lifetime of a service happens between the time onCreate() is called and the time onDestroy()returns. Like an activity, a service does its initial setup in onCreate() and releases all remaining resources inonDestroy(). For example, a music playback service could create the thread where the music will be played inonCreate(), then stop the thread in onDestroy().

The onCreate() and onDestroy() methods are called for all services, whether they‘re created bystartService() or bindService().

1. The active lifetime of a service begins with a call to either onStartCommand() or onBind(). Each method is handed the Intent that was passed to either startService() or bindService(), respectively.

If the service is started, the active lifetime ends the same time that the entire lifetime ends (the service is still active even after onStartCommand() returns). If the service is bound, the active lifetime ends whenonUnbind() returns.

如果该服务已启动,有效寿命结束,整个寿命结束同一时间(甚至在 onstartcommand() 返回服务仍然是激活的)。如果服务绑定,有效寿命结束whenonunbind() 返回。

Note: Although a started service is stopped by a call to either stopSelf() or stopService(), there is not a respective callback for the service (there‘s no onStop() callback). So, unless the service is bound to a client, the system destroys it when the service is stopped—onDestroy() is the only callback received.

注: 虽然一开始的服务是由一个叫到 stopself() 或 stopservice()停了下来,没有服务的相应的回调(没有 onstop() 回调)。所以,除非服务绑定到客户端,系统销毁它当服务是stopped- ondestroy() 是唯一回调接收。

Figure 2 illustrates the typical callback methods for a service. Although the figure separates services that are created by startService() from those created by bindService(), keep in mind that any service, no matter how it‘s started, can potentially allow clients to bind to it. So, a service that was initially started withonStartCommand() (by a client calling startService()) can still receive a call to onBind() (when a client callsbindService()).

图2说明了一个服务的典型回调方法。虽然这是由 startservice() 的bindservice() 创造的人物将服务,请记住,任何服务,不管它是如何开始的,可能允许客户端绑定到它。因此,一种服务,是最初开始withonstartcommand() (通过客户端调用 startservice())仍然可以接收到呼叫 onbind() (当客户callsbindservice())。

For more information about creating a service that provides binding, see the Bound?Services document, which includes more information about the onRebind() callback method in the section about Managing?the?Lifecycle?of?a?Bound?Service.

关于创建提供捆绑服务的更多信息,见 绑定服务 文档,其中包括关于 onrebind() 回调方法在部分关于 管理被绑定服务的生命周期的更多信息。

PREVIOUSNEXT

Except as noted, this content is licensed under Apache?2.0. For details and restrictions, see the Content?License.

除非另有说明,这个内容是在Apache 2许可 。细节和限制,看到 内容许可证。

Android 4.2 r1 - 13 Nov 2012 0:25

About?Android  |  Legal  |  Support

时间: 2024-08-01 10:43:37

API翻译 --- Bound Services的相关文章

Android官方文档之Bound Services

绑定式Service在CS结构中扮演着Server的角色.绑定式Service允许其他组件(如Activity)绑定该Service.发送请求.接收响应.甚至IPC通信( interprocess communication).绑定式Service通常服务于其他应用程序的组件.且没有明确的后台的概念(does not run in the background indefinitely). 本文将介绍bound Service的相关内容,包括其创建.与其他应用组件如何绑定 等.有关Service

Android开发-API指南-Bound 类型的服务

Bound Services 英文原文:http://developer.android.com/guide/components/bound-services.html 采集(更新)日期:2014-12-24 原博客:http://blog.sina.com.cn/s/blog_48d4913001010696.html 在本文中 简介 创建一个 Bound 服务 扩展 Binder 类 使用 Messenger 绑定一个服务 管理 Bound 服务的生命周期 关键类 Service Serv

IOS---UICOLLECTIONVIEW详解和常用API翻译

IOS---UICOLLECTIONVIEW详解和常用API翻译 UICollectionView 1.必须要设置布局参数 2.注册cell 用法类似于UITableView 类.自动实现重用,必须注册初始化. 使用UICollectionView必须实现UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout这三个协议. Collection View的构成,我们能看到的有三个部

iOS开发-NSData全部API翻译学习

学习NSData,发现网上资料比较零碎,还是苹果官方文档比较全面细致,非常适合集中学习,以下是NSData全部API翻译,欢迎转载学习. /**************** Immutable Data 不可变的Data ****************/ @interface NSData : NSObject <NSCopying, NSMutableCopying, NSSecureCoding> //6.获取data长度的属性 @property (readonly) NSUInteg

2.App Components-Services/Bound Services

1. Bound Services A bound service is the server in a client-server interface. A bound service allows components (such as activities) to bind to the service, send requests, receive responses, and even perform interprocess communication (IPC). A bound

angular -- $route API翻译

$route -$routeProvider服务 -依赖ngRoute模块 $route能够在路径发生改变的时候,渲染不同的视图,调用不同的控制器.它监测了$location.url(),然后根据路径来匹配相应的路由,路由的定义方法详见$routeProvider的API. $route通常和$routeProvider服务和ngView指令一起使用 依赖: $location$routeParams 方法: reload() 在路由没有改变的时候,再次加载当前路径的路由,重新渲染ng-view

angular -- $routeParams API翻译

原api出处: https://docs.angularjs.org/api/ngRoute/service/$routeParams $routeParams 可以获取当前路径参数. 需要ngroute模块被安装. 路径参数是$location.search()和$location.path()的组合. 当$route被匹配的时候,路径参数就会被获取. 在参数名称冲突的情况下,路径参数优先于搜索参数. $routeParams服务保证当路由发生变化时,$routeParams对象的id不会变化

蓝牙API翻译____①_____android.bluetooth介绍

前言:最近在学习蓝牙开发,借此机会翻译下官网文档中关于蓝牙的部分,然后学习下如何利用蓝牙开发蓝牙聊天软件和文件传输软件,顺便提升自己的英文文档阅读能力. 英文原地址:  http://www.androidcommunitydocs.com/reference/android/bluetooth/package-summary.html 正文: android.bluetooth包提供了一些管理蓝牙功能的类,例如扫描附近的蓝牙设备.连接设备以及管理设备间的数据传输. 如果需要更详细的信息,可以在

API翻译 --- Intent and Intent Filters

IN THIS DOCUMENT Intent Types           目的类型 Building an Intent        构建一个意图 Example explicit intent       例子显式意图 Example implicit intent      例隐式意图 Forcing an app chooser 迫使应用程序选择器 Receiving an Implicit Intent           收到一个隐式意图 Example filters