19066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/*
29066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Copyright (C) 2006 The Android Open Source Project
39066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
49066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
59066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * you may not use this file except in compliance with the License.
69066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * You may obtain a copy of the License at
79066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
89066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
99066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Unless required by applicable law or agreed to in writing, software
119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * See the License for the specific language governing permissions and
149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * limitations under the License.
159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */
169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectpackage android.app;
189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
19c68c913d357e2955d4bd7ca52829071e531c7825Dianne Hackbornimport android.content.ComponentCallbacks2;
209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.content.ComponentName;
219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.content.Intent;
229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.content.ContextWrapper;
239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.content.Context;
249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.content.res.Configuration;
25f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackbornimport android.os.Build;
269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.os.RemoteException;
279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.os.IBinder;
28d8a43f61680bacf0d4b52a03ff3c7a07307377fcDianne Hackbornimport android.util.Log;
299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.io.FileDescriptor;
319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.io.PrintWriter;
329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/**
34ee3bcc4c6462d1402e48e9d260e16d038d8fe291Dianne Hackborn * A Service is an application component representing either an application's desire
35ee3bcc4c6462d1402e48e9d260e16d038d8fe291Dianne Hackborn * to perform a longer-running operation while not interacting with the user
36ee3bcc4c6462d1402e48e9d260e16d038d8fe291Dianne Hackborn * or to supply functionality for other applications to use.  Each service
379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * class must have a corresponding
389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * {@link android.R.styleable#AndroidManifestService <service>}
399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * declaration in its package's <code>AndroidManifest.xml</code>.  Services
409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * can be started with
419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * {@link android.content.Context#startService Context.startService()} and
429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * {@link android.content.Context#bindService Context.bindService()}.
439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * <p>Note that services, like other application objects, run in the main
459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * thread of their hosting process.  This means that, if your service is going
469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * to do any CPU intensive (such as MP3 playback) or blocking (such as
479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * networking) operations, it should spawn its own thread in which to do that
489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * work.  More information on this can be found in
497aee61f5a96e94e158bf5ad3d8e192c4d4f7eff6Scott Main * <a href="{@docRoot}guide/topics/fundamentals/processes-and-threads.html">Processes and
507aee61f5a96e94e158bf5ad3d8e192c4d4f7eff6Scott Main * Threads</a>.  The {@link IntentService} class is available
51ee3bcc4c6462d1402e48e9d260e16d038d8fe291Dianne Hackborn * as a standard implementation of Service that has its own thread where it
52ee3bcc4c6462d1402e48e9d260e16d038d8fe291Dianne Hackborn * schedules its work to be done.</p>
539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * <p>Topics covered here:
559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * <ol>
56ee3bcc4c6462d1402e48e9d260e16d038d8fe291Dianne Hackborn * <li><a href="#WhatIsAService">What is a Service?</a>
579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * <li><a href="#ServiceLifecycle">Service Lifecycle</a>
589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * <li><a href="#Permissions">Permissions</a>
599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * <li><a href="#ProcessLifecycle">Process Lifecycle</a>
6075288fa1a4ee4886959af7243995d8afd9c3c905Dianne Hackborn * <li><a href="#LocalServiceSample">Local Service Sample</a>
6175288fa1a4ee4886959af7243995d8afd9c3c905Dianne Hackborn * <li><a href="#RemoteMessengerServiceSample">Remote Messenger Service Sample</a>
629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * </ol>
63b54e7a3d9f60ac605f404f9eb3c5e92ca51bbd23Joe Fernandez *
64b54e7a3d9f60ac605f404f9eb3c5e92ca51bbd23Joe Fernandez * <div class="special reference">
65b54e7a3d9f60ac605f404f9eb3c5e92ca51bbd23Joe Fernandez * <h3>Developer Guides</h3>
66b54e7a3d9f60ac605f404f9eb3c5e92ca51bbd23Joe Fernandez * <p>For a detailed discussion about how to create services, read the
67b54e7a3d9f60ac605f404f9eb3c5e92ca51bbd23Joe Fernandez * <a href="{@docRoot}guide/topics/fundamentals/services.html">Services</a> developer guide.</p>
68b54e7a3d9f60ac605f404f9eb3c5e92ca51bbd23Joe Fernandez * </div>
69b54e7a3d9f60ac605f404f9eb3c5e92ca51bbd23Joe Fernandez *
70ee3bcc4c6462d1402e48e9d260e16d038d8fe291Dianne Hackborn * <a name="WhatIsAService"></a>
71ee3bcc4c6462d1402e48e9d260e16d038d8fe291Dianne Hackborn * <h3>What is a Service?</h3>
72ee3bcc4c6462d1402e48e9d260e16d038d8fe291Dianne Hackborn *
73ee3bcc4c6462d1402e48e9d260e16d038d8fe291Dianne Hackborn * <p>Most confusion about the Service class actually revolves around what
74ee3bcc4c6462d1402e48e9d260e16d038d8fe291Dianne Hackborn * it is <em>not</em>:</p>
75ee3bcc4c6462d1402e48e9d260e16d038d8fe291Dianne Hackborn *
76ee3bcc4c6462d1402e48e9d260e16d038d8fe291Dianne Hackborn * <ul>
77ee3bcc4c6462d1402e48e9d260e16d038d8fe291Dianne Hackborn * <li> A Service is <b>not</b> a separate process.  The Service object itself
78ee3bcc4c6462d1402e48e9d260e16d038d8fe291Dianne Hackborn * does not imply it is running in its own process; unless otherwise specified,
79ee3bcc4c6462d1402e48e9d260e16d038d8fe291Dianne Hackborn * it runs in the same process as the application it is part of.
80ee3bcc4c6462d1402e48e9d260e16d038d8fe291Dianne Hackborn * <li> A Service is <b>not</b> a thread.  It is not a means itself to do work off
81ee3bcc4c6462d1402e48e9d260e16d038d8fe291Dianne Hackborn * of the main thread (to avoid Application Not Responding errors).
82ee3bcc4c6462d1402e48e9d260e16d038d8fe291Dianne Hackborn * </ul>
83ee3bcc4c6462d1402e48e9d260e16d038d8fe291Dianne Hackborn *
84ee3bcc4c6462d1402e48e9d260e16d038d8fe291Dianne Hackborn * <p>Thus a Service itself is actually very simple, providing two main features:</p>
85ee3bcc4c6462d1402e48e9d260e16d038d8fe291Dianne Hackborn *
86ee3bcc4c6462d1402e48e9d260e16d038d8fe291Dianne Hackborn * <ul>
87ee3bcc4c6462d1402e48e9d260e16d038d8fe291Dianne Hackborn * <li>A facility for the application to tell the system <em>about</em>
88ee3bcc4c6462d1402e48e9d260e16d038d8fe291Dianne Hackborn * something it wants to be doing in the background (even when the user is not
89ee3bcc4c6462d1402e48e9d260e16d038d8fe291Dianne Hackborn * directly interacting with the application).  This corresponds to calls to
90ee3bcc4c6462d1402e48e9d260e16d038d8fe291Dianne Hackborn * {@link android.content.Context#startService Context.startService()}, which
91ee3bcc4c6462d1402e48e9d260e16d038d8fe291Dianne Hackborn * ask the system to schedule work for the service, to be run until the service
92ee3bcc4c6462d1402e48e9d260e16d038d8fe291Dianne Hackborn * or someone else explicitly stop it.
93ee3bcc4c6462d1402e48e9d260e16d038d8fe291Dianne Hackborn * <li>A facility for an application to expose some of its functionality to
94ee3bcc4c6462d1402e48e9d260e16d038d8fe291Dianne Hackborn * other applications.  This corresponds to calls to
95ee3bcc4c6462d1402e48e9d260e16d038d8fe291Dianne Hackborn * {@link android.content.Context#bindService Context.bindService()}, which
96ee3bcc4c6462d1402e48e9d260e16d038d8fe291Dianne Hackborn * allows a long-standing connection to be made to the service in order to
97ee3bcc4c6462d1402e48e9d260e16d038d8fe291Dianne Hackborn * interact with it.
98ee3bcc4c6462d1402e48e9d260e16d038d8fe291Dianne Hackborn * </ul>
99ee3bcc4c6462d1402e48e9d260e16d038d8fe291Dianne Hackborn *
100ee3bcc4c6462d1402e48e9d260e16d038d8fe291Dianne Hackborn * <p>When a Service component is actually created, for either of these reasons,
101ee3bcc4c6462d1402e48e9d260e16d038d8fe291Dianne Hackborn * all that the system actually does is instantiate the component
102ee3bcc4c6462d1402e48e9d260e16d038d8fe291Dianne Hackborn * and call its {@link #onCreate} and any other appropriate callbacks on the
103ee3bcc4c6462d1402e48e9d260e16d038d8fe291Dianne Hackborn * main thread.  It is up to the Service to implement these with the appropriate
104ee3bcc4c6462d1402e48e9d260e16d038d8fe291Dianne Hackborn * behavior, such as creating a secondary thread in which it does its work.</p>
105ee3bcc4c6462d1402e48e9d260e16d038d8fe291Dianne Hackborn *
106ee3bcc4c6462d1402e48e9d260e16d038d8fe291Dianne Hackborn * <p>Note that because Service itself is so simple, you can make your
107ee3bcc4c6462d1402e48e9d260e16d038d8fe291Dianne Hackborn * interaction with it as simple or complicated as you want: from treating it
108ee3bcc4c6462d1402e48e9d260e16d038d8fe291Dianne Hackborn * as a local Java object that you make direct method calls on (as illustrated
109ee3bcc4c6462d1402e48e9d260e16d038d8fe291Dianne Hackborn * by <a href="#LocalServiceSample">Local Service Sample</a>), to providing
110ee3bcc4c6462d1402e48e9d260e16d038d8fe291Dianne Hackborn * a full remoteable interface using AIDL.</p>
111ee3bcc4c6462d1402e48e9d260e16d038d8fe291Dianne Hackborn *
1129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * <a name="ServiceLifecycle"></a>
1139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * <h3>Service Lifecycle</h3>
1149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
1159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * <p>There are two reasons that a service can be run by the system.  If someone
1169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * calls {@link android.content.Context#startService Context.startService()} then the system will
1179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * retrieve the service (creating it and calling its {@link #onCreate} method
118fed534ee5d47a96c1d104f9bd303e9480102813cDianne Hackborn * if needed) and then call its {@link #onStartCommand} method with the
1199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * arguments supplied by the client.  The service will at this point continue
1209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * running until {@link android.content.Context#stopService Context.stopService()} or
1219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * {@link #stopSelf()} is called.  Note that multiple calls to
1229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Context.startService() do not nest (though they do result in multiple corresponding
123fed534ee5d47a96c1d104f9bd303e9480102813cDianne Hackborn * calls to onStartCommand()), so no matter how many times it is started a service
124fed534ee5d47a96c1d104f9bd303e9480102813cDianne Hackborn * will be stopped once Context.stopService() or stopSelf() is called; however,
125fed534ee5d47a96c1d104f9bd303e9480102813cDianne Hackborn * services can use their {@link #stopSelf(int)} method to ensure the service is
126fed534ee5d47a96c1d104f9bd303e9480102813cDianne Hackborn * not stopped until started intents have been processed.
127fed534ee5d47a96c1d104f9bd303e9480102813cDianne Hackborn *
128fed534ee5d47a96c1d104f9bd303e9480102813cDianne Hackborn * <p>For started services, there are two additional major modes of operation
129fed534ee5d47a96c1d104f9bd303e9480102813cDianne Hackborn * they can decide to run in, depending on the value they return from
130fed534ee5d47a96c1d104f9bd303e9480102813cDianne Hackborn * onStartCommand(): {@link #START_STICKY} is used for services that are
131fed534ee5d47a96c1d104f9bd303e9480102813cDianne Hackborn * explicitly started and stopped as needed, while {@link #START_NOT_STICKY}
132fed534ee5d47a96c1d104f9bd303e9480102813cDianne Hackborn * or {@link #START_REDELIVER_INTENT} are used for services that should only
133fed534ee5d47a96c1d104f9bd303e9480102813cDianne Hackborn * remain running while processing any commands sent to them.  See the linked
134fed534ee5d47a96c1d104f9bd303e9480102813cDianne Hackborn * documentation for more detail on the semantics.
1359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
1369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * <p>Clients can also use {@link android.content.Context#bindService Context.bindService()} to
1379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * obtain a persistent connection to a service.  This likewise creates the
1389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * service if it is not already running (calling {@link #onCreate} while
139fed534ee5d47a96c1d104f9bd303e9480102813cDianne Hackborn * doing so), but does not call onStartCommand().  The client will receive the
1409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * {@link android.os.IBinder} object that the service returns from its
1419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * {@link #onBind} method, allowing the client to then make calls back
1429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * to the service.  The service will remain running as long as the connection
1439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * is established (whether or not the client retains a reference on the
1449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * service's IBinder).  Usually the IBinder returned is for a complex
14540eee61e25fb887f5267686f8a0a7c5bd9f95769Scott Main * interface that has been <a href="{@docRoot}guide/components/aidl.html">written
1469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * in aidl</a>.
1479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
1489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * <p>A service can be both started and have connections bound to it.  In such
1499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * a case, the system will keep the service running as long as either it is
1509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * started <em>or</em> there are one or more connections to it with the
1519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * {@link android.content.Context#BIND_AUTO_CREATE Context.BIND_AUTO_CREATE}
1529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * flag.  Once neither
1539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * of these situations hold, the service's {@link #onDestroy} method is called
1549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * and the service is effectively terminated.  All cleanup (stopping threads,
1559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * unregistering receivers) should be complete upon returning from onDestroy().
1569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
1579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * <a name="Permissions"></a>
1589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * <h3>Permissions</h3>
1599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
1609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * <p>Global access to a service can be enforced when it is declared in its
1619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * manifest's {@link android.R.styleable#AndroidManifestService &lt;service&gt;}
1629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * tag.  By doing so, other applications will need to declare a corresponding
1639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * {@link android.R.styleable#AndroidManifestUsesPermission &lt;uses-permission&gt;}
1649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * element in their own manifest to be able to start, stop, or bind to
1659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * the service.
16621c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn *
16721c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn * <p>As of {@link android.os.Build.VERSION_CODES#GINGERBREAD}, when using
16821c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn * {@link Context#startService(Intent) Context.startService(Intent)}, you can
16921c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn * also set {@link Intent#FLAG_GRANT_READ_URI_PERMISSION
17021c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn * Intent.FLAG_GRANT_READ_URI_PERMISSION} and/or {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION
17121c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn * Intent.FLAG_GRANT_WRITE_URI_PERMISSION} on the Intent.  This will grant the
17221c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn * Service temporary access to the specific URIs in the Intent.  Access will
17321c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn * remain until the Service has called {@link #stopSelf(int)} for that start
17421c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn * command or a later one, or until the Service has been completely stopped.
17521c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn * This works for granting access to the other apps that have not requested
17621c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn * the permission protecting the Service, or even when the Service is not
17721c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn * exported at all.
17821c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn *
1799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * <p>In addition, a service can protect individual IPC calls into it with
1809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * permissions, by calling the
1819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * {@link #checkCallingPermission}
1829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * method before executing the implementation of that call.
1839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
1849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * <p>See the <a href="{@docRoot}guide/topics/security/security.html">Security and Permissions</a>
1859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * document for more information on permissions and security in general.
1869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
1879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * <a name="ProcessLifecycle"></a>
1889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * <h3>Process Lifecycle</h3>
1899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
1909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * <p>The Android system will attempt to keep the process hosting a service
1919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * around as long as the service has been started or has clients bound to it.
1929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * When running low on memory and needing to kill existing processes, the
1939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * priority of a process hosting the service will be the higher of the
1949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * following possibilities:
195ab4a81b3c625e33d04ae8070fcce6b6baee6522cDianne Hackborn *
1969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * <ul>
1979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * <li><p>If the service is currently executing code in its
198fed534ee5d47a96c1d104f9bd303e9480102813cDianne Hackborn * {@link #onCreate onCreate()}, {@link #onStartCommand onStartCommand()},
1999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * or {@link #onDestroy onDestroy()} methods, then the hosting process will
2009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * be a foreground process to ensure this code can execute without
2019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * being killed.
2029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * <li><p>If the service has been started, then its hosting process is considered
2039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * to be less important than any processes that are currently visible to the
2049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * user on-screen, but more important than any process not visible.  Because
2059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * only a few processes are generally visible to the user, this means that
206ab4a81b3c625e33d04ae8070fcce6b6baee6522cDianne Hackborn * the service should not be killed except in low memory conditions.  However, since
207ab4a81b3c625e33d04ae8070fcce6b6baee6522cDianne Hackborn * the user is not directly aware of a background service, in that state it <em>is</em>
208ab4a81b3c625e33d04ae8070fcce6b6baee6522cDianne Hackborn * considered a valid candidate to kill, and you should be prepared for this to
209ab4a81b3c625e33d04ae8070fcce6b6baee6522cDianne Hackborn * happen.  In particular, long-running services will be increasingly likely to
210ab4a81b3c625e33d04ae8070fcce6b6baee6522cDianne Hackborn * kill and are guaranteed to be killed (and restarted if appropriate) if they
211ab4a81b3c625e33d04ae8070fcce6b6baee6522cDianne Hackborn * remain started long enough.
2129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * <li><p>If there are clients bound to the service, then the service's hosting
2139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * process is never less important than the most important client.  That is,
2149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * if one of its clients is visible to the user, then the service itself is
215ab4a81b3c625e33d04ae8070fcce6b6baee6522cDianne Hackborn * considered to be visible.  The way a client's importance impacts the service's
216ab4a81b3c625e33d04ae8070fcce6b6baee6522cDianne Hackborn * importance can be adjusted through {@link Context#BIND_ABOVE_CLIENT},
217ab4a81b3c625e33d04ae8070fcce6b6baee6522cDianne Hackborn * {@link Context#BIND_ALLOW_OOM_MANAGEMENT}, {@link Context#BIND_WAIVE_PRIORITY},
218ab4a81b3c625e33d04ae8070fcce6b6baee6522cDianne Hackborn * {@link Context#BIND_IMPORTANT}, and {@link Context#BIND_ADJUST_WITH_ACTIVITY}.
219fed534ee5d47a96c1d104f9bd303e9480102813cDianne Hackborn * <li><p>A started service can use the {@link #startForeground(int, Notification)}
220fed534ee5d47a96c1d104f9bd303e9480102813cDianne Hackborn * API to put the service in a foreground state, where the system considers
221fed534ee5d47a96c1d104f9bd303e9480102813cDianne Hackborn * it to be something the user is actively aware of and thus not a candidate
222fed534ee5d47a96c1d104f9bd303e9480102813cDianne Hackborn * for killing when low on memory.  (It is still theoretically possible for
223fed534ee5d47a96c1d104f9bd303e9480102813cDianne Hackborn * the service to be killed under extreme memory pressure from the current
224fed534ee5d47a96c1d104f9bd303e9480102813cDianne Hackborn * foreground application, but in practice this should not be a concern.)
2259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * </ul>
2269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
2279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * <p>Note this means that most of the time your service is running, it may
2289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * be killed by the system if it is under heavy memory pressure.  If this
2299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * happens, the system will later try to restart the service.  An important
230fed534ee5d47a96c1d104f9bd303e9480102813cDianne Hackborn * consequence of this is that if you implement {@link #onStartCommand onStartCommand()}
2319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * to schedule work to be done asynchronously or in another thread, then you
23229e4a3c566f435c32f0b95e4ac8e8b33cac6fabaDianne Hackborn * may want to use {@link #START_FLAG_REDELIVERY} to have the system
23329e4a3c566f435c32f0b95e4ac8e8b33cac6fabaDianne Hackborn * re-deliver an Intent for you so that it does not get lost if your service
23429e4a3c566f435c32f0b95e4ac8e8b33cac6fabaDianne Hackborn * is killed while processing it.
2359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
2369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * <p>Other application components running in the same process as the service
2379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * (such as an {@link android.app.Activity}) can, of course, increase the
2389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * importance of the overall
2399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * process beyond just the importance of the service itself.
24075288fa1a4ee4886959af7243995d8afd9c3c905Dianne Hackborn *
24175288fa1a4ee4886959af7243995d8afd9c3c905Dianne Hackborn * <a name="LocalServiceSample"></a>
24275288fa1a4ee4886959af7243995d8afd9c3c905Dianne Hackborn * <h3>Local Service Sample</h3>
24375288fa1a4ee4886959af7243995d8afd9c3c905Dianne Hackborn *
24475288fa1a4ee4886959af7243995d8afd9c3c905Dianne Hackborn * <p>One of the most common uses of a Service is as a secondary component
24575288fa1a4ee4886959af7243995d8afd9c3c905Dianne Hackborn * running alongside other parts of an application, in the same process as
24675288fa1a4ee4886959af7243995d8afd9c3c905Dianne Hackborn * the rest of the components.  All components of an .apk run in the same
24775288fa1a4ee4886959af7243995d8afd9c3c905Dianne Hackborn * process unless explicitly stated otherwise, so this is a typical situation.
24875288fa1a4ee4886959af7243995d8afd9c3c905Dianne Hackborn *
24975288fa1a4ee4886959af7243995d8afd9c3c905Dianne Hackborn * <p>When used in this way, by assuming the
25075288fa1a4ee4886959af7243995d8afd9c3c905Dianne Hackborn * components are in the same process, you can greatly simplify the interaction
25175288fa1a4ee4886959af7243995d8afd9c3c905Dianne Hackborn * between them: clients of the service can simply cast the IBinder they
25275288fa1a4ee4886959af7243995d8afd9c3c905Dianne Hackborn * receive from it to a concrete class published by the service.
25375288fa1a4ee4886959af7243995d8afd9c3c905Dianne Hackborn *
25475288fa1a4ee4886959af7243995d8afd9c3c905Dianne Hackborn * <p>An example of this use of a Service is shown here.  First is the Service
25575288fa1a4ee4886959af7243995d8afd9c3c905Dianne Hackborn * itself, publishing a custom class when bound:
25675288fa1a4ee4886959af7243995d8afd9c3c905Dianne Hackborn *
25775288fa1a4ee4886959af7243995d8afd9c3c905Dianne Hackborn * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/LocalService.java
25875288fa1a4ee4886959af7243995d8afd9c3c905Dianne Hackborn *      service}
25975288fa1a4ee4886959af7243995d8afd9c3c905Dianne Hackborn *
26075288fa1a4ee4886959af7243995d8afd9c3c905Dianne Hackborn * <p>With that done, one can now write client code that directly accesses the
26175288fa1a4ee4886959af7243995d8afd9c3c905Dianne Hackborn * running service, such as:
26275288fa1a4ee4886959af7243995d8afd9c3c905Dianne Hackborn *
26375288fa1a4ee4886959af7243995d8afd9c3c905Dianne Hackborn * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/LocalServiceActivities.java
26475288fa1a4ee4886959af7243995d8afd9c3c905Dianne Hackborn *      bind}
26575288fa1a4ee4886959af7243995d8afd9c3c905Dianne Hackborn *
26675288fa1a4ee4886959af7243995d8afd9c3c905Dianne Hackborn * <a name="RemoteMessengerServiceSample"></a>
26775288fa1a4ee4886959af7243995d8afd9c3c905Dianne Hackborn * <h3>Remote Messenger Service Sample</h3>
26875288fa1a4ee4886959af7243995d8afd9c3c905Dianne Hackborn *
26975288fa1a4ee4886959af7243995d8afd9c3c905Dianne Hackborn * <p>If you need to be able to write a Service that can perform complicated
27075288fa1a4ee4886959af7243995d8afd9c3c905Dianne Hackborn * communication with clients in remote processes (beyond simply the use of
27175288fa1a4ee4886959af7243995d8afd9c3c905Dianne Hackborn * {@link Context#startService(Intent) Context.startService} to send
27275288fa1a4ee4886959af7243995d8afd9c3c905Dianne Hackborn * commands to it), then you can use the {@link android.os.Messenger} class
27375288fa1a4ee4886959af7243995d8afd9c3c905Dianne Hackborn * instead of writing full AIDL files.
27475288fa1a4ee4886959af7243995d8afd9c3c905Dianne Hackborn *
27575288fa1a4ee4886959af7243995d8afd9c3c905Dianne Hackborn * <p>An example of a Service that uses Messenger as its client interface
27675288fa1a4ee4886959af7243995d8afd9c3c905Dianne Hackborn * is shown here.  First is the Service itself, publishing a Messenger to
27775288fa1a4ee4886959af7243995d8afd9c3c905Dianne Hackborn * an internal Handler when bound:
27875288fa1a4ee4886959af7243995d8afd9c3c905Dianne Hackborn *
27975288fa1a4ee4886959af7243995d8afd9c3c905Dianne Hackborn * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/MessengerService.java
28075288fa1a4ee4886959af7243995d8afd9c3c905Dianne Hackborn *      service}
28175288fa1a4ee4886959af7243995d8afd9c3c905Dianne Hackborn *
28275288fa1a4ee4886959af7243995d8afd9c3c905Dianne Hackborn * <p>If we want to make this service run in a remote process (instead of the
28375288fa1a4ee4886959af7243995d8afd9c3c905Dianne Hackborn * standard one for its .apk), we can use <code>android:process</code> in its
28475288fa1a4ee4886959af7243995d8afd9c3c905Dianne Hackborn * manifest tag to specify one:
28575288fa1a4ee4886959af7243995d8afd9c3c905Dianne Hackborn *
28675288fa1a4ee4886959af7243995d8afd9c3c905Dianne Hackborn * {@sample development/samples/ApiDemos/AndroidManifest.xml remote_service_declaration}
28775288fa1a4ee4886959af7243995d8afd9c3c905Dianne Hackborn *
28875288fa1a4ee4886959af7243995d8afd9c3c905Dianne Hackborn * <p>Note that the name "remote" chosen here is arbitrary, and you can use
28975288fa1a4ee4886959af7243995d8afd9c3c905Dianne Hackborn * other names if you want additional processes.  The ':' prefix appends the
29075288fa1a4ee4886959af7243995d8afd9c3c905Dianne Hackborn * name to your package's standard process name.
29175288fa1a4ee4886959af7243995d8afd9c3c905Dianne Hackborn *
29275288fa1a4ee4886959af7243995d8afd9c3c905Dianne Hackborn * <p>With that done, clients can now bind to the service and send messages
29375288fa1a4ee4886959af7243995d8afd9c3c905Dianne Hackborn * to it.  Note that this allows clients to register with it to receive
29475288fa1a4ee4886959af7243995d8afd9c3c905Dianne Hackborn * messages back as well:
29575288fa1a4ee4886959af7243995d8afd9c3c905Dianne Hackborn *
29675288fa1a4ee4886959af7243995d8afd9c3c905Dianne Hackborn * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/MessengerServiceActivities.java
29775288fa1a4ee4886959af7243995d8afd9c3c905Dianne Hackborn *      bind}
2989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */
299c68c913d357e2955d4bd7ca52829071e531c7825Dianne Hackbornpublic abstract class Service extends ContextWrapper implements ComponentCallbacks2 {
3009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private static final String TAG = "Service";
3019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public Service() {
3039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        super(null);
3049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /** Return the application that owns this service. */
3079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public final Application getApplication() {
3089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return mApplication;
3099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
3129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Called by the system when the service is first created.  Do not call this method directly.
3139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
3149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public void onCreate() {
3159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
318f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn     * @deprecated Implement {@link #onStartCommand(Intent, int, int)} instead.
319f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn     */
320f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn    @Deprecated
321f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn    public void onStart(Intent intent, int startId) {
322f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn    }
323f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn
324f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn    /**
325f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn     * Bits returned by {@link #onStartCommand} describing how to continue
326f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn     * the service if it is killed.  May be {@link #START_STICKY},
327f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn     * {@link #START_NOT_STICKY}, {@link #START_REDELIVER_INTENT},
328f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn     * or {@link #START_STICKY_COMPATIBILITY}.
329f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn     */
330f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn    public static final int START_CONTINUATION_MASK = 0xf;
331f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn
332f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn    /**
333f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn     * Constant to return from {@link #onStartCommand}: compatibility
334f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn     * version of {@link #START_STICKY} that does not guarantee that
335f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn     * {@link #onStartCommand} will be called again after being killed.
336f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn     */
337f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn    public static final int START_STICKY_COMPATIBILITY = 0;
338f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn
339f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn    /**
340f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn     * Constant to return from {@link #onStartCommand}: if this service's
341f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn     * process is killed while it is started (after returning from
342f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn     * {@link #onStartCommand}), then leave it in the started state but
343f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn     * don't retain this delivered intent.  Later the system will try to
344fed534ee5d47a96c1d104f9bd303e9480102813cDianne Hackborn     * re-create the service.  Because it is in the started state, it will
345fed534ee5d47a96c1d104f9bd303e9480102813cDianne Hackborn     * guarantee to call {@link #onStartCommand} after creating the new
346fed534ee5d47a96c1d104f9bd303e9480102813cDianne Hackborn     * service instance; if there are not any pending start commands to be
347fed534ee5d47a96c1d104f9bd303e9480102813cDianne Hackborn     * delivered to the service, it will be called with a null intent
348fed534ee5d47a96c1d104f9bd303e9480102813cDianne Hackborn     * object, so you must take care to check for this.
349f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn     *
350f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn     * <p>This mode makes sense for things that will be explicitly started
351f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn     * and stopped to run for arbitrary periods of time, such as a service
352f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn     * performing background music playback.
353f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn     */
354f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn    public static final int START_STICKY = 1;
355f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn
356f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn    /**
357f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn     * Constant to return from {@link #onStartCommand}: if this service's
358f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn     * process is killed while it is started (after returning from
359f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn     * {@link #onStartCommand}), and there are no new start intents to
360f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn     * deliver to it, then take the service out of the started state and
361f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn     * don't recreate until a future explicit call to
36229e4a3c566f435c32f0b95e4ac8e8b33cac6fabaDianne Hackborn     * {@link Context#startService Context.startService(Intent)}.  The
36329e4a3c566f435c32f0b95e4ac8e8b33cac6fabaDianne Hackborn     * service will not receive a {@link #onStartCommand(Intent, int, int)}
36429e4a3c566f435c32f0b95e4ac8e8b33cac6fabaDianne Hackborn     * call with a null Intent because it will not be re-started if there
36529e4a3c566f435c32f0b95e4ac8e8b33cac6fabaDianne Hackborn     * are no pending Intents to deliver.
366f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn     *
367f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn     * <p>This mode makes sense for things that want to do some work as a
368f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn     * result of being started, but can be stopped when under memory pressure
369f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn     * and will explicit start themselves again later to do more work.  An
370f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn     * example of such a service would be one that polls for data from
371f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn     * a server: it could schedule an alarm to poll every N minutes by having
372f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn     * the alarm start its service.  When its {@link #onStartCommand} is
373f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn     * called from the alarm, it schedules a new alarm for N minutes later,
374f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn     * and spawns a thread to do its networking.  If its process is killed
375f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn     * while doing that check, the service will not be restarted until the
376f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn     * alarm goes off.
377f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn     */
378f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn    public static final int START_NOT_STICKY = 2;
379f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn
380f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn    /**
381f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn     * Constant to return from {@link #onStartCommand}: if this service's
382f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn     * process is killed while it is started (after returning from
383f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn     * {@link #onStartCommand}), then it will be scheduled for a restart
384f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn     * and the last delivered Intent re-delivered to it again via
385f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn     * {@link #onStartCommand}.  This Intent will remain scheduled for
386f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn     * redelivery until the service calls {@link #stopSelf(int)} with the
38729e4a3c566f435c32f0b95e4ac8e8b33cac6fabaDianne Hackborn     * start ID provided to {@link #onStartCommand}.  The
38829e4a3c566f435c32f0b95e4ac8e8b33cac6fabaDianne Hackborn     * service will not receive a {@link #onStartCommand(Intent, int, int)}
38929e4a3c566f435c32f0b95e4ac8e8b33cac6fabaDianne Hackborn     * call with a null Intent because it will will only be re-started if
39029e4a3c566f435c32f0b95e4ac8e8b33cac6fabaDianne Hackborn     * it is not finished processing all Intents sent to it (and any such
39129e4a3c566f435c32f0b95e4ac8e8b33cac6fabaDianne Hackborn     * pending events will be delivered at the point of restart).
392f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn     */
393f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn    public static final int START_REDELIVER_INTENT = 3;
394f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn
395f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn    /**
3960c5001d776d56bae02a5cc2663286a125d99bc5eDianne Hackborn     * Special constant for reporting that we are done processing
3970c5001d776d56bae02a5cc2663286a125d99bc5eDianne Hackborn     * {@link #onTaskRemoved(Intent)}.
3980c5001d776d56bae02a5cc2663286a125d99bc5eDianne Hackborn     * @hide
3990c5001d776d56bae02a5cc2663286a125d99bc5eDianne Hackborn     */
4000c5001d776d56bae02a5cc2663286a125d99bc5eDianne Hackborn    public static final int START_TASK_REMOVED_COMPLETE = 1000;
4010c5001d776d56bae02a5cc2663286a125d99bc5eDianne Hackborn
4020c5001d776d56bae02a5cc2663286a125d99bc5eDianne Hackborn    /**
403f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn     * This flag is set in {@link #onStartCommand} if the Intent is a
404f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn     * re-delivery of a previously delivered intent, because the service
405f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn     * had previously returned {@link #START_REDELIVER_INTENT} but had been
406f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn     * killed before calling {@link #stopSelf(int)} for that Intent.
407f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn     */
408f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn    public static final int START_FLAG_REDELIVERY = 0x0001;
409f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn
410f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn    /**
411f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn     * This flag is set in {@link #onStartCommand} if the Intent is a
412b8d07179340b823d32a1a05426e691353903e990Evan Charlton     * retry because the original attempt never got to or returned from
413f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn     * {@link #onStartCommand(Intent, int, int)}.
414f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn     */
415f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn    public static final int START_FLAG_RETRY = 0x0002;
416f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn
417f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn    /**
4189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Called by the system every time a client explicitly starts the service by calling
4199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * {@link android.content.Context#startService}, providing the arguments it supplied and a
4209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * unique integer token representing the start request.  Do not call this method directly.
421f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn     *
422f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn     * <p>For backwards compatibility, the default implementation calls
423f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn     * {@link #onStart} and returns either {@link #START_STICKY}
424f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn     * or {@link #START_STICKY_COMPATIBILITY}.
425f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn     *
4260766b2d0f398dcad10e332b695bbc0cbe5011882Dianne Hackborn     * <p>If you need your application to run on platform versions prior to API
4270766b2d0f398dcad10e332b695bbc0cbe5011882Dianne Hackborn     * level 5, you can use the following model to handle the older {@link #onStart}
4280766b2d0f398dcad10e332b695bbc0cbe5011882Dianne Hackborn     * callback in that case.  The <code>handleCommand</code> method is implemented by
4290766b2d0f398dcad10e332b695bbc0cbe5011882Dianne Hackborn     * you as appropriate:
4300766b2d0f398dcad10e332b695bbc0cbe5011882Dianne Hackborn     *
431ab8a8ed2eb068b696f6b5519c55a03546a5927efDianne Hackborn     * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/ForegroundService.java
432ab8a8ed2eb068b696f6b5519c55a03546a5927efDianne Hackborn     *   start_compatibility}
4330166c3530535355e7813f54c4e403a21db94a9d2Brad Fitzpatrick     *
4340166c3530535355e7813f54c4e403a21db94a9d2Brad Fitzpatrick     * <p class="caution">Note that the system calls this on your
4350166c3530535355e7813f54c4e403a21db94a9d2Brad Fitzpatrick     * service's main thread.  A service's main thread is the same
436ee34a49ffc92590cb59f3e17a3df136b67701529Brad Fitzpatrick     * thread where UI operations take place for Activities running in the
4370166c3530535355e7813f54c4e403a21db94a9d2Brad Fitzpatrick     * same process.  You should always avoid stalling the main
4380166c3530535355e7813f54c4e403a21db94a9d2Brad Fitzpatrick     * thread's event loop.  When doing long-running operations,
4390166c3530535355e7813f54c4e403a21db94a9d2Brad Fitzpatrick     * network calls, or heavy disk I/O, you should kick off a new
4400166c3530535355e7813f54c4e403a21db94a9d2Brad Fitzpatrick     * thread, or use {@link android.os.AsyncTask}.</p>
4410166c3530535355e7813f54c4e403a21db94a9d2Brad Fitzpatrick     *
4429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param intent The Intent supplied to {@link android.content.Context#startService},
443f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn     * as given.  This may be null if the service is being restarted after
444f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn     * its process has gone away, and it had previously returned anything
445f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn     * except {@link #START_STICKY_COMPATIBILITY}.
446f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn     * @param flags Additional data about this start request.  Currently either
447f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn     * 0, {@link #START_FLAG_REDELIVERY}, or {@link #START_FLAG_RETRY}.
4489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param startId A unique integer representing this specific request to
449f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn     * start.  Use with {@link #stopSelfResult(int)}.
450f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn     *
451f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn     * @return The return value indicates what semantics the system should
452f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn     * use for the service's current started state.  It may be one of the
453f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn     * constants associated with the {@link #START_CONTINUATION_MASK} bits.
4549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
4559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #stopSelfResult(int)
4569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
457f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn    public int onStartCommand(Intent intent, int flags, int startId) {
458f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn        onStart(intent, startId);
459f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn        return mStartCompatibility ? START_STICKY_COMPATIBILITY : START_STICKY;
4609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
461f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn
4629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
4639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Called by the system to notify a Service that it is no longer used and is being removed.  The
464f76a50ce8fdc6aea22cabc77b2977a1a15a79630Ken Wakasa     * service should clean up any resources it holds (threads, registered
4659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * receivers, etc) at this point.  Upon return, there will be no more calls
4669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * in to this Service object and it is effectively dead.  Do not call this method directly.
4679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
4689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public void onDestroy() {
4699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public void onConfigurationChanged(Configuration newConfig) {
4729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public void onLowMemory() {
4759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
476c68c913d357e2955d4bd7ca52829071e531c7825Dianne Hackborn
477c68c913d357e2955d4bd7ca52829071e531c7825Dianne Hackborn    public void onTrimMemory(int level) {
478c68c913d357e2955d4bd7ca52829071e531c7825Dianne Hackborn    }
479c68c913d357e2955d4bd7ca52829071e531c7825Dianne Hackborn
4809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
4819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Return the communication channel to the service.  May return null if
4829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * clients can not bind to the service.  The returned
4839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * {@link android.os.IBinder} is usually for a complex interface
48440eee61e25fb887f5267686f8a0a7c5bd9f95769Scott Main     * that has been <a href="{@docRoot}guide/components/aidl.html">described using
4859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * aidl</a>.
4869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
4879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * <p><em>Note that unlike other application components, calls on to the
4889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * IBinder interface returned here may not happen on the main thread
4897aee61f5a96e94e158bf5ad3d8e192c4d4f7eff6Scott Main     * of the process</em>.  More information about the main thread can be found in
4907aee61f5a96e94e158bf5ad3d8e192c4d4f7eff6Scott Main     * <a href="{@docRoot}guide/topics/fundamentals/processes-and-threads.html">Processes and
4917aee61f5a96e94e158bf5ad3d8e192c4d4f7eff6Scott Main     * Threads</a>.</p>
4929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
4939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param intent The Intent that was used to bind to this service,
4949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * as given to {@link android.content.Context#bindService
4959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Context.bindService}.  Note that any extras that were included with
4969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * the Intent at that point will <em>not</em> be seen here.
4979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
4989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return Return an IBinder through which clients can call on to the
4999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *         service.
5009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
5019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public abstract IBinder onBind(Intent intent);
5029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
5049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Called when all clients have disconnected from a particular interface
5059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * published by the service.  The default implementation does nothing and
5069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * returns false.
5079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
5089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param intent The Intent that was used to bind to this service,
5099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * as given to {@link android.content.Context#bindService
5109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Context.bindService}.  Note that any extras that were included with
5119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * the Intent at that point will <em>not</em> be seen here.
5129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
5139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return Return true if you would like to have the service's
5149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * {@link #onRebind} method later called when new clients bind to it.
5159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
5169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public boolean onUnbind(Intent intent) {
5179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return false;
5189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
5199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
5219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Called when new clients have connected to the service, after it had
5229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * previously been notified that all had disconnected in its
5239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * {@link #onUnbind}.  This will only be called if the implementation
5249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * of {@link #onUnbind} was overridden to return true.
5259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
5269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param intent The Intent that was used to bind to this service,
5279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * as given to {@link android.content.Context#bindService
5289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Context.bindService}.  Note that any extras that were included with
5299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * the Intent at that point will <em>not</em> be seen here.
5309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
5319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public void onRebind(Intent intent) {
5329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
5339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
5350c5001d776d56bae02a5cc2663286a125d99bc5eDianne Hackborn     * This is called if the service is currently running and the user has
5360c5001d776d56bae02a5cc2663286a125d99bc5eDianne Hackborn     * removed a task that comes from the service's application.  If you have
5370c5001d776d56bae02a5cc2663286a125d99bc5eDianne Hackborn     * set {@link android.content.pm.ServiceInfo#FLAG_STOP_WITH_TASK ServiceInfo.FLAG_STOP_WITH_TASK}
5380c5001d776d56bae02a5cc2663286a125d99bc5eDianne Hackborn     * then you will not receive this callback; instead, the service will simply
5390c5001d776d56bae02a5cc2663286a125d99bc5eDianne Hackborn     * be stopped.
5400c5001d776d56bae02a5cc2663286a125d99bc5eDianne Hackborn     *
5410c5001d776d56bae02a5cc2663286a125d99bc5eDianne Hackborn     * @param rootIntent The original root Intent that was used to launch
5420c5001d776d56bae02a5cc2663286a125d99bc5eDianne Hackborn     * the task that is being removed.
5430c5001d776d56bae02a5cc2663286a125d99bc5eDianne Hackborn     */
5440c5001d776d56bae02a5cc2663286a125d99bc5eDianne Hackborn    public void onTaskRemoved(Intent rootIntent) {
5450c5001d776d56bae02a5cc2663286a125d99bc5eDianne Hackborn    }
5460c5001d776d56bae02a5cc2663286a125d99bc5eDianne Hackborn
5470c5001d776d56bae02a5cc2663286a125d99bc5eDianne Hackborn    /**
5489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Stop the service, if it was previously started.  This is the same as
5499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * calling {@link android.content.Context#stopService} for this particular service.
5509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
5519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #stopSelfResult(int)
5529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
5539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public final void stopSelf() {
5549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        stopSelf(-1);
5559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
5569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
5589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Old version of {@link #stopSelfResult} that doesn't return a result.
5599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
5609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #stopSelfResult
5619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
5629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public final void stopSelf(int startId) {
5639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (mActivityManager == null) {
5649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return;
5659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
5669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        try {
5679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            mActivityManager.stopServiceToken(
5689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    new ComponentName(this, mClassName), mToken, startId);
5699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } catch (RemoteException ex) {
5709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
5719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
5729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
574105925376f8d0f6b318c9938c7b83ef7fef094daThe Android Open Source Project     * Stop the service if the most recent time it was started was
5759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * <var>startId</var>.  This is the same as calling {@link
5769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * android.content.Context#stopService} for this particular service but allows you to
5779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * safely avoid stopping if there is a start request from a client that you
578105925376f8d0f6b318c9938c7b83ef7fef094daThe Android Open Source Project     * haven't yet seen in {@link #onStart}.
5799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
58029e4a3c566f435c32f0b95e4ac8e8b33cac6fabaDianne Hackborn     * <p><em>Be careful about ordering of your calls to this function.</em>.
58129e4a3c566f435c32f0b95e4ac8e8b33cac6fabaDianne Hackborn     * If you call this function with the most-recently received ID before
58229e4a3c566f435c32f0b95e4ac8e8b33cac6fabaDianne Hackborn     * you have called it for previously received IDs, the service will be
58329e4a3c566f435c32f0b95e4ac8e8b33cac6fabaDianne Hackborn     * immediately stopped anyway.  If you may end up processing IDs out
58429e4a3c566f435c32f0b95e4ac8e8b33cac6fabaDianne Hackborn     * of order (such as by dispatching them on separate threads), then you
58529e4a3c566f435c32f0b95e4ac8e8b33cac6fabaDianne Hackborn     * are responsible for stopping them in the same order you received them.</p>
58629e4a3c566f435c32f0b95e4ac8e8b33cac6fabaDianne Hackborn     *
5879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param startId The most recent start identifier received in {@link
5889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *                #onStart}.
5899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return Returns true if the startId matches the last start request
5909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * and the service will be stopped, else false.
5919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
5929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #stopSelf()
5939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
5949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public final boolean stopSelfResult(int startId) {
5959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (mActivityManager == null) {
5969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return false;
5979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
5989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        try {
5999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return mActivityManager.stopServiceToken(
6009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    new ComponentName(this, mClassName), mToken, startId);
6019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } catch (RemoteException ex) {
6029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
6039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return false;
6049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
6059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
607d8a43f61680bacf0d4b52a03ff3c7a07307377fcDianne Hackborn     * @deprecated This is a now a no-op, use
60829e4a3c566f435c32f0b95e4ac8e8b33cac6fabaDianne Hackborn     * {@link #startForeground(int, Notification)} instead.  This method
60929e4a3c566f435c32f0b95e4ac8e8b33cac6fabaDianne Hackborn     * has been turned into a no-op rather than simply being deprecated
61029e4a3c566f435c32f0b95e4ac8e8b33cac6fabaDianne Hackborn     * because analysis of numerous poorly behaving devices has shown that
61129e4a3c566f435c32f0b95e4ac8e8b33cac6fabaDianne Hackborn     * increasingly often the trouble is being caused in part by applications
61229e4a3c566f435c32f0b95e4ac8e8b33cac6fabaDianne Hackborn     * that are abusing it.  Thus, given a choice between introducing
61329e4a3c566f435c32f0b95e4ac8e8b33cac6fabaDianne Hackborn     * problems in existing applications using this API (by allowing them to
61429e4a3c566f435c32f0b95e4ac8e8b33cac6fabaDianne Hackborn     * be killed when they would like to avoid it), vs allowing the performance
61529e4a3c566f435c32f0b95e4ac8e8b33cac6fabaDianne Hackborn     * of the entire system to be decreased, this method was deemed less
61629e4a3c566f435c32f0b95e4ac8e8b33cac6fabaDianne Hackborn     * important.
6174f3867e3ce92101224ad79b8f2ff446bb4f99108Dianne Hackborn     *
6184f3867e3ce92101224ad79b8f2ff446bb4f99108Dianne Hackborn     * @hide
619d8a43f61680bacf0d4b52a03ff3c7a07307377fcDianne Hackborn     */
620d8a43f61680bacf0d4b52a03ff3c7a07307377fcDianne Hackborn    @Deprecated
621d8a43f61680bacf0d4b52a03ff3c7a07307377fcDianne Hackborn    public final void setForeground(boolean isForeground) {
622d8a43f61680bacf0d4b52a03ff3c7a07307377fcDianne Hackborn        Log.w(TAG, "setForeground: ignoring old API call on " + getClass().getName());
623d8a43f61680bacf0d4b52a03ff3c7a07307377fcDianne Hackborn    }
624d8a43f61680bacf0d4b52a03ff3c7a07307377fcDianne Hackborn
625d8a43f61680bacf0d4b52a03ff3c7a07307377fcDianne Hackborn    /**
626d8a43f61680bacf0d4b52a03ff3c7a07307377fcDianne Hackborn     * Make this service run in the foreground, supplying the ongoing
627d8a43f61680bacf0d4b52a03ff3c7a07307377fcDianne Hackborn     * notification to be shown to the user while in this state.
6289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * By default services are background, meaning that if the system needs to
6299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * kill them to reclaim more memory (such as to display a large page in a
6309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * web browser), they can be killed without too much harm.  You can set this
631d8a43f61680bacf0d4b52a03ff3c7a07307377fcDianne Hackborn     * flag if killing your service would be disruptive to the user, such as
6329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * if your service is performing background music playback, so the user
6339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * would notice if their music stopped playing.
6349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
6350766b2d0f398dcad10e332b695bbc0cbe5011882Dianne Hackborn     * <p>If you need your application to run on platform versions prior to API
6364f3867e3ce92101224ad79b8f2ff446bb4f99108Dianne Hackborn     * level 5, you can use the following model to call the the older setForeground()
6370766b2d0f398dcad10e332b695bbc0cbe5011882Dianne Hackborn     * or this modern method as appropriate:
6380766b2d0f398dcad10e332b695bbc0cbe5011882Dianne Hackborn     *
639ab8a8ed2eb068b696f6b5519c55a03546a5927efDianne Hackborn     * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/ForegroundService.java
640ab8a8ed2eb068b696f6b5519c55a03546a5927efDianne Hackborn     *   foreground_compatibility}
6410766b2d0f398dcad10e332b695bbc0cbe5011882Dianne Hackborn     *
642d8a43f61680bacf0d4b52a03ff3c7a07307377fcDianne Hackborn     * @param id The identifier for this notification as per
643d8a43f61680bacf0d4b52a03ff3c7a07307377fcDianne Hackborn     * {@link NotificationManager#notify(int, Notification)
6442e6de8fdccf71dc733864c1ea757182cc13303abScott Main     * NotificationManager.notify(int, Notification)}; must not be 0.
645d8a43f61680bacf0d4b52a03ff3c7a07307377fcDianne Hackborn     * @param notification The Notification to be displayed.
646d8a43f61680bacf0d4b52a03ff3c7a07307377fcDianne Hackborn     *
647d8a43f61680bacf0d4b52a03ff3c7a07307377fcDianne Hackborn     * @see #stopForeground(boolean)
6489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
649d8a43f61680bacf0d4b52a03ff3c7a07307377fcDianne Hackborn    public final void startForeground(int id, Notification notification) {
650d8a43f61680bacf0d4b52a03ff3c7a07307377fcDianne Hackborn        try {
651d8a43f61680bacf0d4b52a03ff3c7a07307377fcDianne Hackborn            mActivityManager.setServiceForeground(
652d8a43f61680bacf0d4b52a03ff3c7a07307377fcDianne Hackborn                    new ComponentName(this, mClassName), mToken, id,
653d8a43f61680bacf0d4b52a03ff3c7a07307377fcDianne Hackborn                    notification, true);
654d8a43f61680bacf0d4b52a03ff3c7a07307377fcDianne Hackborn        } catch (RemoteException ex) {
6559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
656d8a43f61680bacf0d4b52a03ff3c7a07307377fcDianne Hackborn    }
657d8a43f61680bacf0d4b52a03ff3c7a07307377fcDianne Hackborn
658d8a43f61680bacf0d4b52a03ff3c7a07307377fcDianne Hackborn    /**
659d8a43f61680bacf0d4b52a03ff3c7a07307377fcDianne Hackborn     * Remove this service from foreground state, allowing it to be killed if
660d8a43f61680bacf0d4b52a03ff3c7a07307377fcDianne Hackborn     * more memory is needed.
6611066cbcac08268e2254ed6818181949d83e9ba1cDianne Hackborn     * @param removeNotification If true, the notification previously provided
6621066cbcac08268e2254ed6818181949d83e9ba1cDianne Hackborn     * to {@link #startForeground} will be removed.  Otherwise it will remain
6631066cbcac08268e2254ed6818181949d83e9ba1cDianne Hackborn     * until a later call removes it (or the service is destroyed).
664d8a43f61680bacf0d4b52a03ff3c7a07307377fcDianne Hackborn     * @see #startForeground(int, Notification)
665d8a43f61680bacf0d4b52a03ff3c7a07307377fcDianne Hackborn     */
666d8a43f61680bacf0d4b52a03ff3c7a07307377fcDianne Hackborn    public final void stopForeground(boolean removeNotification) {
6679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        try {
6689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            mActivityManager.setServiceForeground(
669d8a43f61680bacf0d4b52a03ff3c7a07307377fcDianne Hackborn                    new ComponentName(this, mClassName), mToken, 0, null,
670d8a43f61680bacf0d4b52a03ff3c7a07307377fcDianne Hackborn                    removeNotification);
6719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } catch (RemoteException ex) {
6729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
6739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
6749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
6769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Print the Service's state into the given stream.  This gets invoked if
677f6dc1078091f33f231fd30830c618f6a04932f82kmccormick     * you run "adb shell dumpsys activity service &lt;yourservicename&gt;"
678f6dc1078091f33f231fd30830c618f6a04932f82kmccormick     * (note that for this command to work, the service must be running, and
679f6dc1078091f33f231fd30830c618f6a04932f82kmccormick     * you must specify a fully-qualified service name).
6805554b7082220d37496e30f39a0d9146afc177ab4Jeff Sharkey     * This is distinct from "dumpsys &lt;servicename&gt;", which only works for
6819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * named system services and which invokes the {@link IBinder#dump} method
6829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * on the {@link IBinder} interface registered with ServiceManager.
6839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
6849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param fd The raw file descriptor that the dump is being sent to.
6859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param writer The PrintWriter to which you should dump your state.  This will be
6869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * closed for you after you return.
6879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param args additional arguments to the dump request.
6889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
6899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
6909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        writer.println("nothing to dump");
6919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
6929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // ------------------ Internal API ------------------
6949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
6969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @hide
6979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
6989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public final void attach(
6999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            Context context,
7009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            ActivityThread thread, String className, IBinder token,
7019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            Application application, Object activityManager) {
7029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        attachBaseContext(context);
7039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mThread = thread;           // NOTE:  unused - remove?
7049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mClassName = className;
7059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mToken = token;
7069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mApplication = application;
7079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mActivityManager = (IActivityManager)activityManager;
708f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn        mStartCompatibility = getApplicationInfo().targetSdkVersion
709f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn                < Build.VERSION_CODES.ECLAIR;
7109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
7119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    final String getClassName() {
7139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return mClassName;
7149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
7159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // set by the thread after the constructor and before onCreate(Bundle icicle) is called.
7179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private ActivityThread mThread = null;
7189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private String mClassName = null;
7199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private IBinder mToken = null;
7209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private Application mApplication = null;
7219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private IActivityManager mActivityManager = null;
722f6f9f2d0256930ce0bb4913b2260b8480914edc2Dianne Hackborn    private boolean mStartCompatibility = false;
7239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
724