Lines Matching refs:service

44 <p>A bound service is the server in a client-server interface. A bound service allows components
45 (such as activities) to bind to the service, send requests, receive responses, and even perform
46 interprocess communication (IPC). A bound service typically lives only while it serves another
49 <p>This document shows you how to create a bound service, including how to bind
50 to the service from other application components. However, you should also refer to the <a
52 information about services in general, such as how to deliver notifications from a service, set
53 the service to run in the foreground, and more.</p>
58 <p>A bound service is an implementation of the {@link android.app.Service} class that allows
60 service, you must implement the {@link android.app.Service#onBind onBind()} callback method. This
62 clients can use to interact with the service.</p>
69 document, you can create a service that is both started and bound. That is, the service can be
71 service to run indefinitely, and also allow a client to bind to the service by calling {@link
73 <p>If you do allow your service to be started and bound, then when the service has been
74 started, the system does <em>not</em> destroy the service when all clients unbind. Instead, you must
75 explicitly stop the service, by calling {@link android.app.Service#stopSelf stopSelf()} or {@link
80 implement both. For example, a music player might find it useful to allow its service to run
81 indefinitely and also provide binding. This way, an activity can start the service to play some
83 returns to the application, the activity can bind to the service to regain control of playback.</p>
86 Service</a>, for more information about the service lifecycle when adding binding to a
87 started service.</p>
91 <p>A client can bind to the service by calling {@link android.content.Context#bindService
93 android.content.ServiceConnection}, which monitors the connection with the service. The {@link
96 client and service, it calls {@link
99 the client can use to communicate with the service.</p>
101 <p>Multiple clients can connect to the service at once. However, the system calls your service's
106 <p>When the last client unbinds from the service, the system destroys the service (unless the
107 service was also started by {@link android.content.Context#startService startService()}).</p>
109 <p>When you implement your bound service, the most important part is defining the interface
111 different ways you can define your service's {@link android.os.IBinder} interface and the following
118 <p>When creating a service that provides binding, you must provide an {@link android.os.IBinder}
119 that provides the programming interface that clients can use to interact with the service. There
124 <dd>If your service is private to your own application and runs in the same process as the client
130 <p>This is the preferred technique when your service is merely a background worker for your own
132 your service is used by other applications or across separate processes.</dd>
136 an interface for the service with a {@link android.os.Messenger}. In this manner, the service
140 with the client, allowing the client to send commands to the service using {@link
142 its own so the service can send messages back.
145 your service to be thread-safe.</p>
153 all the client requests in a single thread, so the service receives requests one at a time. If,
154 however, you want your service to handle multiple requests simultaneously, then you can use AIDL
155 directly. In this case, your service must be capable of multi-threading and be built thread-safe.
159 can then extend within your service.</p>
164 create a bound service, because it may require multithreading capabilities and
166 and this document does not discuss how to use it for your service. If you're certain that you need
175 <p>If your service is used only by the local application and does not need to work across processes,
177 access to public methods in the service.</p>
179 <p class="note"><strong>Note:</strong> This works only if the client and service are in the same
181 application that needs to bind an activity to its own service that's playing music in the
186 <li>In your service, create an instance of {@link android.os.Binder} that either:
191 <li>or, returns an instance of another class hosted by the service with public methods the
198 make calls to the bound service using the methods provided.</li>
201 <p class="note"><strong>Note:</strong> The reason the service and client must be in the same
202 application is so the client can cast the returned object and properly call its APIs. The service
206 <p>For example, here's a service that provides clients access to methods in the service through
217 * Class used for the client Binder. Because we know this service always
241 service. For example, clients can call {@code getRandomNumber()} from the service.</p>
268 // Unbind from the service
287 /** Defines callbacks for service binding, passed to bindService() */
292 IBinder service) {
294 LocalBinder binder = (LocalBinder) service;
307 <p>The above sample shows how the client binds to the service using an implementation of
310 section provides more information about this process of binding to the service.</p>
312 <p class="note"><strong>Note:</strong> The example above doesn't explicitly unbind from the service,
333 all calls to the service, whereas, a pure AIDL interface sends simultaneous requests to the
334 service, which must then handle multi-threading.</p>
335 <p>For most applications, the service doesn't need to perform multi-threading, so using a {@link
336 android.os.Messenger} allows the service to handle one call at a time. If it's important
337 that your service be multi-threaded, then you should use <a
342 <p>If you need your service to communicate with remote processes, then you can use a
343 {@link android.os.Messenger} to provide the interface for your service. This technique allows
349 <li>The service implements a {@link android.os.Handler} that receives a callback for each
353 <li>The {@link android.os.Messenger} creates an {@link android.os.IBinder} that the service
356 (that references the service's {@link android.os.Handler}), which the client uses to send
357 {@link android.os.Message} objects to the service.</li>
358 <li>The service receives each {@link android.os.Message} in its {@link
364 <p>In this way, there are no "methods" for the client to call on the service. Instead, the
365 client delivers "messages" ({@link android.os.Message} objects) that the service receives in
368 <p>Here's a simple example service that uses a {@link android.os.Messenger} interface:</p>
372 /** Command to the service to display a message */
397 * When binding to the service, we return an interface to our messenger
398 * for sending messages to the service.
409 {@link android.os.Handler} is where the service receives the incoming {@link android.os.Message}
413 android.os.IBinder} returned by the service and send a message using {@link
415 service and delivers the {@code MSG_SAY_HELLO} message to the service:</p>
419 /** Messenger for communicating with the service. */
422 /** Flag indicating whether we have called bind on the service. */
426 * Class for interacting with the main interface of the service.
429 public void onServiceConnected(ComponentName className, IBinder service) {
430 // This is called when the connection with the service has been
432 // interact with the service. We are communicating with the
433 // service using a Messenger, so here we get a client-side
435 mService = new Messenger(service);
440 // This is called when the connection with the service has been
449 // Create and send a message to the service, using a supported 'what' value
467 // Bind to the service
475 // Unbind from the service
484 <p>Notice that this example does not show how the service can respond to the client. If you want the
485 service to respond, then you need to also create a {@link android.os.Messenger} in the client. Then
487 onServiceConnected()} callback, it sends a {@link android.os.Message} to the service that includes
493 MessengerService.java}</a> (service) and <a
503 <p>Application components (clients) can bind to a service by calling
505 system then calls the service's {@link android.app.Service#onBind
506 onBind()} method, which returns an {@link android.os.IBinder} for interacting with the service.</p>
516 to a service&mdash;you <strong>cannot</strong> bind to a service from a broadcast receiver.</p>
518 <p>So, to bind to a service from your client, you must: </p>
525 the service's {@link android.app.Service#onBind onBind()} method.</dd>
528 <dd>The Android system calls this when the connection to the service is unexpectedly
529 lost, such as when the service has crashed or has been killed. This is <em>not</em> called when the
537 onServiceConnected()} callback method, you can begin making calls to the service, using
539 <li>To disconnect from the service, call {@link
541 <p>When your client is destroyed, it will unbind from the service, but you should always unbind
542 when you're done interacting with the service or when your activity pauses so that the service can
548 <p>For example, the following snippet connects the client to the service created above by
556 // Called when the connection with the service is established
557 public void onServiceConnected(ComponentName className, IBinder service) {
559 // service that is running in our own process, we can
561 LocalBinder binder = (LocalBinder) service;
566 // Called when the connection with the service disconnects unexpectedly
574 <p>With this {@link android.content.ServiceConnection}, the client can bind to a service by passing
584 {@link android.content.Intent} that explicitly names the service to bind (thought the intent
588 android.content.Context#BIND_AUTO_CREATE} in order to create the service if its not already alive.
596 <p>Here are some important notes about binding to a service:</p>
604 <li>If you only need to interact with the service while your activity is visible, you
610 activity needs to use the service the entire time it's running (even in the background), so if
611 the service is in another process, then you increase the weight of the process and it becomes
618 multiple activities in your application bind to the same service and there is a transition between
619 two of those activities, the service may be destroyed and recreated as the current activity unbinds
626 <p>For more sample code, showing how to bind to a service, see the <a
637 <p>When a service is unbound from all clients, the Android system destroys it (unless it was also
639 to manage the lifecycle of your service if it's purely a bound
640 service&mdash;the Android system manages it for you based on whether it is bound to any clients.</p>
643 onStartCommand()} callback method, then you must explicitly stop the service, because the
644 service is now considered to be <em>started</em>. In this case, the service runs until the service
649 <p>Additionally, if your service is started and accepts binding, then when the system calls
652 onRebind()} the next time a client binds to the service. {@link android.app.Service#onRebind
659 <p class="img-caption"><strong>Figure 1.</strong> The lifecycle for a service that is started
663 <p>For more information about the lifecycle of a started service, see the <a