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