The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.app; |
| 18 | |
Dianne Hackborn | 67324c9 | 2016-04-18 13:55:25 -0700 | [diff] [blame] | 19 | import android.annotation.IntDef; |
Scott Kennedy | 7db73ed | 2015-02-19 15:31:11 -0800 | [diff] [blame] | 20 | import android.annotation.Nullable; |
Mathew Inwood | 4fb17d1 | 2018-08-14 14:25:44 +0100 | [diff] [blame] | 21 | import android.annotation.UnsupportedAppUsage; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 22 | import android.content.ComponentCallbacks2; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | import android.content.ComponentName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | import android.content.Context; |
kopriva | 762a08f | 2018-10-03 16:09:03 -0700 | [diff] [blame] | 25 | import android.content.ContextWrapper; |
| 26 | import android.content.Intent; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | import android.content.res.Configuration; |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 28 | import android.os.Build; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | import android.os.IBinder; |
kopriva | 762a08f | 2018-10-03 16:09:03 -0700 | [diff] [blame] | 30 | import android.os.RemoteException; |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 31 | import android.util.Log; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | |
| 33 | import java.io.FileDescriptor; |
| 34 | import java.io.PrintWriter; |
Dianne Hackborn | 67324c9 | 2016-04-18 13:55:25 -0700 | [diff] [blame] | 35 | import java.lang.annotation.Retention; |
| 36 | import java.lang.annotation.RetentionPolicy; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | |
| 38 | /** |
Dianne Hackborn | ee3bcc4 | 2010-04-15 11:33:38 -0700 | [diff] [blame] | 39 | * A Service is an application component representing either an application's desire |
| 40 | * to perform a longer-running operation while not interacting with the user |
| 41 | * or to supply functionality for other applications to use. Each service |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 42 | * class must have a corresponding |
| 43 | * {@link android.R.styleable#AndroidManifestService <service>} |
| 44 | * declaration in its package's <code>AndroidManifest.xml</code>. Services |
| 45 | * can be started with |
| 46 | * {@link android.content.Context#startService Context.startService()} and |
| 47 | * {@link android.content.Context#bindService Context.bindService()}. |
| 48 | * |
| 49 | * <p>Note that services, like other application objects, run in the main |
| 50 | * thread of their hosting process. This means that, if your service is going |
| 51 | * to do any CPU intensive (such as MP3 playback) or blocking (such as |
| 52 | * networking) operations, it should spawn its own thread in which to do that |
| 53 | * work. More information on this can be found in |
Scott Main | 7aee61f | 2011-02-08 11:25:01 -0800 | [diff] [blame] | 54 | * <a href="{@docRoot}guide/topics/fundamentals/processes-and-threads.html">Processes and |
| 55 | * Threads</a>. The {@link IntentService} class is available |
Dianne Hackborn | ee3bcc4 | 2010-04-15 11:33:38 -0700 | [diff] [blame] | 56 | * as a standard implementation of Service that has its own thread where it |
| 57 | * schedules its work to be done.</p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | * |
| 59 | * <p>Topics covered here: |
| 60 | * <ol> |
Dianne Hackborn | ee3bcc4 | 2010-04-15 11:33:38 -0700 | [diff] [blame] | 61 | * <li><a href="#WhatIsAService">What is a Service?</a> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 62 | * <li><a href="#ServiceLifecycle">Service Lifecycle</a> |
| 63 | * <li><a href="#Permissions">Permissions</a> |
| 64 | * <li><a href="#ProcessLifecycle">Process Lifecycle</a> |
Dianne Hackborn | 75288fa | 2010-02-16 18:01:18 -0800 | [diff] [blame] | 65 | * <li><a href="#LocalServiceSample">Local Service Sample</a> |
| 66 | * <li><a href="#RemoteMessengerServiceSample">Remote Messenger Service Sample</a> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 67 | * </ol> |
Joe Fernandez | b54e7a3 | 2011-10-03 15:09:50 -0700 | [diff] [blame] | 68 | * |
| 69 | * <div class="special reference"> |
| 70 | * <h3>Developer Guides</h3> |
| 71 | * <p>For a detailed discussion about how to create services, read the |
| 72 | * <a href="{@docRoot}guide/topics/fundamentals/services.html">Services</a> developer guide.</p> |
| 73 | * </div> |
| 74 | * |
Dianne Hackborn | ee3bcc4 | 2010-04-15 11:33:38 -0700 | [diff] [blame] | 75 | * <a name="WhatIsAService"></a> |
| 76 | * <h3>What is a Service?</h3> |
| 77 | * |
| 78 | * <p>Most confusion about the Service class actually revolves around what |
| 79 | * it is <em>not</em>:</p> |
| 80 | * |
| 81 | * <ul> |
| 82 | * <li> A Service is <b>not</b> a separate process. The Service object itself |
| 83 | * does not imply it is running in its own process; unless otherwise specified, |
| 84 | * it runs in the same process as the application it is part of. |
| 85 | * <li> A Service is <b>not</b> a thread. It is not a means itself to do work off |
| 86 | * of the main thread (to avoid Application Not Responding errors). |
| 87 | * </ul> |
| 88 | * |
| 89 | * <p>Thus a Service itself is actually very simple, providing two main features:</p> |
| 90 | * |
| 91 | * <ul> |
| 92 | * <li>A facility for the application to tell the system <em>about</em> |
| 93 | * something it wants to be doing in the background (even when the user is not |
| 94 | * directly interacting with the application). This corresponds to calls to |
| 95 | * {@link android.content.Context#startService Context.startService()}, which |
| 96 | * ask the system to schedule work for the service, to be run until the service |
| 97 | * or someone else explicitly stop it. |
| 98 | * <li>A facility for an application to expose some of its functionality to |
| 99 | * other applications. This corresponds to calls to |
| 100 | * {@link android.content.Context#bindService Context.bindService()}, which |
| 101 | * allows a long-standing connection to be made to the service in order to |
| 102 | * interact with it. |
| 103 | * </ul> |
| 104 | * |
| 105 | * <p>When a Service component is actually created, for either of these reasons, |
| 106 | * all that the system actually does is instantiate the component |
| 107 | * and call its {@link #onCreate} and any other appropriate callbacks on the |
| 108 | * main thread. It is up to the Service to implement these with the appropriate |
| 109 | * behavior, such as creating a secondary thread in which it does its work.</p> |
| 110 | * |
| 111 | * <p>Note that because Service itself is so simple, you can make your |
| 112 | * interaction with it as simple or complicated as you want: from treating it |
| 113 | * as a local Java object that you make direct method calls on (as illustrated |
| 114 | * by <a href="#LocalServiceSample">Local Service Sample</a>), to providing |
| 115 | * a full remoteable interface using AIDL.</p> |
| 116 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 117 | * <a name="ServiceLifecycle"></a> |
| 118 | * <h3>Service Lifecycle</h3> |
| 119 | * |
| 120 | * <p>There are two reasons that a service can be run by the system. If someone |
| 121 | * calls {@link android.content.Context#startService Context.startService()} then the system will |
| 122 | * retrieve the service (creating it and calling its {@link #onCreate} method |
Dianne Hackborn | fed534e | 2009-09-23 00:42:12 -0700 | [diff] [blame] | 123 | * if needed) and then call its {@link #onStartCommand} method with the |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 124 | * arguments supplied by the client. The service will at this point continue |
| 125 | * running until {@link android.content.Context#stopService Context.stopService()} or |
| 126 | * {@link #stopSelf()} is called. Note that multiple calls to |
| 127 | * Context.startService() do not nest (though they do result in multiple corresponding |
Dianne Hackborn | fed534e | 2009-09-23 00:42:12 -0700 | [diff] [blame] | 128 | * calls to onStartCommand()), so no matter how many times it is started a service |
| 129 | * will be stopped once Context.stopService() or stopSelf() is called; however, |
| 130 | * services can use their {@link #stopSelf(int)} method to ensure the service is |
| 131 | * not stopped until started intents have been processed. |
| 132 | * |
| 133 | * <p>For started services, there are two additional major modes of operation |
| 134 | * they can decide to run in, depending on the value they return from |
| 135 | * onStartCommand(): {@link #START_STICKY} is used for services that are |
| 136 | * explicitly started and stopped as needed, while {@link #START_NOT_STICKY} |
| 137 | * or {@link #START_REDELIVER_INTENT} are used for services that should only |
| 138 | * remain running while processing any commands sent to them. See the linked |
| 139 | * documentation for more detail on the semantics. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 140 | * |
| 141 | * <p>Clients can also use {@link android.content.Context#bindService Context.bindService()} to |
| 142 | * obtain a persistent connection to a service. This likewise creates the |
| 143 | * service if it is not already running (calling {@link #onCreate} while |
Dianne Hackborn | fed534e | 2009-09-23 00:42:12 -0700 | [diff] [blame] | 144 | * doing so), but does not call onStartCommand(). The client will receive the |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 145 | * {@link android.os.IBinder} object that the service returns from its |
| 146 | * {@link #onBind} method, allowing the client to then make calls back |
| 147 | * to the service. The service will remain running as long as the connection |
| 148 | * is established (whether or not the client retains a reference on the |
| 149 | * service's IBinder). Usually the IBinder returned is for a complex |
Scott Main | 40eee61 | 2012-08-06 17:48:37 -0700 | [diff] [blame] | 150 | * interface that has been <a href="{@docRoot}guide/components/aidl.html">written |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 151 | * in aidl</a>. |
| 152 | * |
| 153 | * <p>A service can be both started and have connections bound to it. In such |
| 154 | * a case, the system will keep the service running as long as either it is |
| 155 | * started <em>or</em> there are one or more connections to it with the |
| 156 | * {@link android.content.Context#BIND_AUTO_CREATE Context.BIND_AUTO_CREATE} |
| 157 | * flag. Once neither |
| 158 | * of these situations hold, the service's {@link #onDestroy} method is called |
| 159 | * and the service is effectively terminated. All cleanup (stopping threads, |
| 160 | * unregistering receivers) should be complete upon returning from onDestroy(). |
| 161 | * |
| 162 | * <a name="Permissions"></a> |
| 163 | * <h3>Permissions</h3> |
| 164 | * |
| 165 | * <p>Global access to a service can be enforced when it is declared in its |
| 166 | * manifest's {@link android.R.styleable#AndroidManifestService <service>} |
| 167 | * tag. By doing so, other applications will need to declare a corresponding |
| 168 | * {@link android.R.styleable#AndroidManifestUsesPermission <uses-permission>} |
| 169 | * element in their own manifest to be able to start, stop, or bind to |
| 170 | * the service. |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 171 | * |
| 172 | * <p>As of {@link android.os.Build.VERSION_CODES#GINGERBREAD}, when using |
| 173 | * {@link Context#startService(Intent) Context.startService(Intent)}, you can |
| 174 | * also set {@link Intent#FLAG_GRANT_READ_URI_PERMISSION |
| 175 | * Intent.FLAG_GRANT_READ_URI_PERMISSION} and/or {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION |
| 176 | * Intent.FLAG_GRANT_WRITE_URI_PERMISSION} on the Intent. This will grant the |
| 177 | * Service temporary access to the specific URIs in the Intent. Access will |
| 178 | * remain until the Service has called {@link #stopSelf(int)} for that start |
| 179 | * command or a later one, or until the Service has been completely stopped. |
| 180 | * This works for granting access to the other apps that have not requested |
| 181 | * the permission protecting the Service, or even when the Service is not |
| 182 | * exported at all. |
| 183 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 184 | * <p>In addition, a service can protect individual IPC calls into it with |
| 185 | * permissions, by calling the |
| 186 | * {@link #checkCallingPermission} |
| 187 | * method before executing the implementation of that call. |
| 188 | * |
| 189 | * <p>See the <a href="{@docRoot}guide/topics/security/security.html">Security and Permissions</a> |
| 190 | * document for more information on permissions and security in general. |
| 191 | * |
| 192 | * <a name="ProcessLifecycle"></a> |
| 193 | * <h3>Process Lifecycle</h3> |
| 194 | * |
| 195 | * <p>The Android system will attempt to keep the process hosting a service |
| 196 | * around as long as the service has been started or has clients bound to it. |
| 197 | * When running low on memory and needing to kill existing processes, the |
| 198 | * priority of a process hosting the service will be the higher of the |
| 199 | * following possibilities: |
Dianne Hackborn | ab4a81b | 2014-10-09 17:59:38 -0700 | [diff] [blame] | 200 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 201 | * <ul> |
| 202 | * <li><p>If the service is currently executing code in its |
Dianne Hackborn | fed534e | 2009-09-23 00:42:12 -0700 | [diff] [blame] | 203 | * {@link #onCreate onCreate()}, {@link #onStartCommand onStartCommand()}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 204 | * or {@link #onDestroy onDestroy()} methods, then the hosting process will |
| 205 | * be a foreground process to ensure this code can execute without |
| 206 | * being killed. |
| 207 | * <li><p>If the service has been started, then its hosting process is considered |
| 208 | * to be less important than any processes that are currently visible to the |
| 209 | * user on-screen, but more important than any process not visible. Because |
| 210 | * only a few processes are generally visible to the user, this means that |
Dianne Hackborn | ab4a81b | 2014-10-09 17:59:38 -0700 | [diff] [blame] | 211 | * the service should not be killed except in low memory conditions. However, since |
| 212 | * the user is not directly aware of a background service, in that state it <em>is</em> |
| 213 | * considered a valid candidate to kill, and you should be prepared for this to |
| 214 | * happen. In particular, long-running services will be increasingly likely to |
| 215 | * kill and are guaranteed to be killed (and restarted if appropriate) if they |
| 216 | * remain started long enough. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 217 | * <li><p>If there are clients bound to the service, then the service's hosting |
| 218 | * process is never less important than the most important client. That is, |
| 219 | * if one of its clients is visible to the user, then the service itself is |
Dianne Hackborn | ab4a81b | 2014-10-09 17:59:38 -0700 | [diff] [blame] | 220 | * considered to be visible. The way a client's importance impacts the service's |
| 221 | * importance can be adjusted through {@link Context#BIND_ABOVE_CLIENT}, |
| 222 | * {@link Context#BIND_ALLOW_OOM_MANAGEMENT}, {@link Context#BIND_WAIVE_PRIORITY}, |
| 223 | * {@link Context#BIND_IMPORTANT}, and {@link Context#BIND_ADJUST_WITH_ACTIVITY}. |
Dianne Hackborn | fed534e | 2009-09-23 00:42:12 -0700 | [diff] [blame] | 224 | * <li><p>A started service can use the {@link #startForeground(int, Notification)} |
| 225 | * API to put the service in a foreground state, where the system considers |
| 226 | * it to be something the user is actively aware of and thus not a candidate |
| 227 | * for killing when low on memory. (It is still theoretically possible for |
| 228 | * the service to be killed under extreme memory pressure from the current |
| 229 | * foreground application, but in practice this should not be a concern.) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 230 | * </ul> |
| 231 | * |
| 232 | * <p>Note this means that most of the time your service is running, it may |
| 233 | * be killed by the system if it is under heavy memory pressure. If this |
| 234 | * happens, the system will later try to restart the service. An important |
Dianne Hackborn | fed534e | 2009-09-23 00:42:12 -0700 | [diff] [blame] | 235 | * consequence of this is that if you implement {@link #onStartCommand onStartCommand()} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 236 | * to schedule work to be done asynchronously or in another thread, then you |
Dianne Hackborn | 29e4a3c | 2009-09-30 22:35:40 -0700 | [diff] [blame] | 237 | * may want to use {@link #START_FLAG_REDELIVERY} to have the system |
| 238 | * re-deliver an Intent for you so that it does not get lost if your service |
| 239 | * is killed while processing it. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 240 | * |
| 241 | * <p>Other application components running in the same process as the service |
| 242 | * (such as an {@link android.app.Activity}) can, of course, increase the |
| 243 | * importance of the overall |
| 244 | * process beyond just the importance of the service itself. |
Dianne Hackborn | 75288fa | 2010-02-16 18:01:18 -0800 | [diff] [blame] | 245 | * |
| 246 | * <a name="LocalServiceSample"></a> |
| 247 | * <h3>Local Service Sample</h3> |
| 248 | * |
| 249 | * <p>One of the most common uses of a Service is as a secondary component |
| 250 | * running alongside other parts of an application, in the same process as |
| 251 | * the rest of the components. All components of an .apk run in the same |
| 252 | * process unless explicitly stated otherwise, so this is a typical situation. |
| 253 | * |
| 254 | * <p>When used in this way, by assuming the |
| 255 | * components are in the same process, you can greatly simplify the interaction |
| 256 | * between them: clients of the service can simply cast the IBinder they |
| 257 | * receive from it to a concrete class published by the service. |
| 258 | * |
| 259 | * <p>An example of this use of a Service is shown here. First is the Service |
| 260 | * itself, publishing a custom class when bound: |
| 261 | * |
| 262 | * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/LocalService.java |
| 263 | * service} |
| 264 | * |
| 265 | * <p>With that done, one can now write client code that directly accesses the |
| 266 | * running service, such as: |
| 267 | * |
| 268 | * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/LocalServiceActivities.java |
| 269 | * bind} |
| 270 | * |
| 271 | * <a name="RemoteMessengerServiceSample"></a> |
| 272 | * <h3>Remote Messenger Service Sample</h3> |
| 273 | * |
| 274 | * <p>If you need to be able to write a Service that can perform complicated |
| 275 | * communication with clients in remote processes (beyond simply the use of |
| 276 | * {@link Context#startService(Intent) Context.startService} to send |
| 277 | * commands to it), then you can use the {@link android.os.Messenger} class |
| 278 | * instead of writing full AIDL files. |
| 279 | * |
| 280 | * <p>An example of a Service that uses Messenger as its client interface |
| 281 | * is shown here. First is the Service itself, publishing a Messenger to |
| 282 | * an internal Handler when bound: |
| 283 | * |
| 284 | * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/MessengerService.java |
| 285 | * service} |
| 286 | * |
| 287 | * <p>If we want to make this service run in a remote process (instead of the |
| 288 | * standard one for its .apk), we can use <code>android:process</code> in its |
| 289 | * manifest tag to specify one: |
| 290 | * |
| 291 | * {@sample development/samples/ApiDemos/AndroidManifest.xml remote_service_declaration} |
| 292 | * |
| 293 | * <p>Note that the name "remote" chosen here is arbitrary, and you can use |
| 294 | * other names if you want additional processes. The ':' prefix appends the |
| 295 | * name to your package's standard process name. |
| 296 | * |
| 297 | * <p>With that done, clients can now bind to the service and send messages |
| 298 | * to it. Note that this allows clients to register with it to receive |
| 299 | * messages back as well: |
| 300 | * |
| 301 | * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/MessengerServiceActivities.java |
| 302 | * bind} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 303 | */ |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 304 | public abstract class Service extends ContextWrapper implements ComponentCallbacks2 { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 305 | private static final String TAG = "Service"; |
| 306 | |
Dianne Hackborn | 67324c9 | 2016-04-18 13:55:25 -0700 | [diff] [blame] | 307 | /** |
| 308 | * Flag for {@link #stopForeground(int)}: if set, the notification previously provided |
| 309 | * to {@link #startForeground} will be removed. Otherwise it will remain |
| 310 | * until a later call (to {@link #startForeground(int, Notification)} or |
| 311 | * {@link #stopForeground(int)} removes it, or the service is destroyed. |
| 312 | */ |
| 313 | public static final int STOP_FOREGROUND_REMOVE = 1<<0; |
| 314 | |
| 315 | /** |
| 316 | * Flag for {@link #stopForeground(int)}: if set, the notification previously provided |
| 317 | * to {@link #startForeground} will be detached from the service. Only makes sense |
| 318 | * when {@link #STOP_FOREGROUND_REMOVE} is <b>not</b> set -- in this case, the notification |
| 319 | * will remain shown, but be completely detached from the service and so no longer changed |
| 320 | * except through direct calls to the notification manager. |
| 321 | */ |
| 322 | public static final int STOP_FOREGROUND_DETACH = 1<<1; |
| 323 | |
| 324 | /** @hide */ |
Jeff Sharkey | 6503bd8 | 2017-04-19 23:24:18 -0600 | [diff] [blame] | 325 | @IntDef(flag = true, prefix = { "STOP_FOREGROUND_" }, value = { |
| 326 | STOP_FOREGROUND_REMOVE, |
| 327 | STOP_FOREGROUND_DETACH |
| 328 | }) |
Dianne Hackborn | 67324c9 | 2016-04-18 13:55:25 -0700 | [diff] [blame] | 329 | @Retention(RetentionPolicy.SOURCE) |
| 330 | public @interface StopForegroundFlags {} |
| 331 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 332 | public Service() { |
| 333 | super(null); |
| 334 | } |
| 335 | |
| 336 | /** Return the application that owns this service. */ |
| 337 | public final Application getApplication() { |
| 338 | return mApplication; |
| 339 | } |
| 340 | |
| 341 | /** |
| 342 | * Called by the system when the service is first created. Do not call this method directly. |
| 343 | */ |
| 344 | public void onCreate() { |
| 345 | } |
| 346 | |
| 347 | /** |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 348 | * @deprecated Implement {@link #onStartCommand(Intent, int, int)} instead. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 349 | */ |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 350 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 351 | public void onStart(Intent intent, int startId) { |
| 352 | } |
| 353 | |
| 354 | /** |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 355 | * Bits returned by {@link #onStartCommand} describing how to continue |
| 356 | * the service if it is killed. May be {@link #START_STICKY}, |
| 357 | * {@link #START_NOT_STICKY}, {@link #START_REDELIVER_INTENT}, |
| 358 | * or {@link #START_STICKY_COMPATIBILITY}. |
| 359 | */ |
| 360 | public static final int START_CONTINUATION_MASK = 0xf; |
| 361 | |
| 362 | /** |
| 363 | * Constant to return from {@link #onStartCommand}: compatibility |
| 364 | * version of {@link #START_STICKY} that does not guarantee that |
| 365 | * {@link #onStartCommand} will be called again after being killed. |
| 366 | */ |
| 367 | public static final int START_STICKY_COMPATIBILITY = 0; |
| 368 | |
| 369 | /** |
| 370 | * Constant to return from {@link #onStartCommand}: if this service's |
| 371 | * process is killed while it is started (after returning from |
| 372 | * {@link #onStartCommand}), then leave it in the started state but |
| 373 | * don't retain this delivered intent. Later the system will try to |
Dianne Hackborn | fed534e | 2009-09-23 00:42:12 -0700 | [diff] [blame] | 374 | * re-create the service. Because it is in the started state, it will |
| 375 | * guarantee to call {@link #onStartCommand} after creating the new |
| 376 | * service instance; if there are not any pending start commands to be |
| 377 | * delivered to the service, it will be called with a null intent |
| 378 | * object, so you must take care to check for this. |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 379 | * |
| 380 | * <p>This mode makes sense for things that will be explicitly started |
| 381 | * and stopped to run for arbitrary periods of time, such as a service |
| 382 | * performing background music playback. |
| 383 | */ |
| 384 | public static final int START_STICKY = 1; |
| 385 | |
| 386 | /** |
| 387 | * Constant to return from {@link #onStartCommand}: if this service's |
| 388 | * process is killed while it is started (after returning from |
| 389 | * {@link #onStartCommand}), and there are no new start intents to |
| 390 | * deliver to it, then take the service out of the started state and |
| 391 | * don't recreate until a future explicit call to |
Dianne Hackborn | 29e4a3c | 2009-09-30 22:35:40 -0700 | [diff] [blame] | 392 | * {@link Context#startService Context.startService(Intent)}. The |
| 393 | * service will not receive a {@link #onStartCommand(Intent, int, int)} |
kopriva | 762a08f | 2018-10-03 16:09:03 -0700 | [diff] [blame] | 394 | * call with a null Intent because it will not be restarted if there |
Dianne Hackborn | 29e4a3c | 2009-09-30 22:35:40 -0700 | [diff] [blame] | 395 | * are no pending Intents to deliver. |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 396 | * |
| 397 | * <p>This mode makes sense for things that want to do some work as a |
| 398 | * result of being started, but can be stopped when under memory pressure |
| 399 | * and will explicit start themselves again later to do more work. An |
| 400 | * example of such a service would be one that polls for data from |
| 401 | * a server: it could schedule an alarm to poll every N minutes by having |
| 402 | * the alarm start its service. When its {@link #onStartCommand} is |
| 403 | * called from the alarm, it schedules a new alarm for N minutes later, |
| 404 | * and spawns a thread to do its networking. If its process is killed |
| 405 | * while doing that check, the service will not be restarted until the |
| 406 | * alarm goes off. |
| 407 | */ |
| 408 | public static final int START_NOT_STICKY = 2; |
Dianne Hackborn | 67324c9 | 2016-04-18 13:55:25 -0700 | [diff] [blame] | 409 | |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 410 | /** |
| 411 | * Constant to return from {@link #onStartCommand}: if this service's |
| 412 | * process is killed while it is started (after returning from |
| 413 | * {@link #onStartCommand}), then it will be scheduled for a restart |
| 414 | * and the last delivered Intent re-delivered to it again via |
| 415 | * {@link #onStartCommand}. This Intent will remain scheduled for |
| 416 | * redelivery until the service calls {@link #stopSelf(int)} with the |
Dianne Hackborn | 29e4a3c | 2009-09-30 22:35:40 -0700 | [diff] [blame] | 417 | * start ID provided to {@link #onStartCommand}. The |
| 418 | * service will not receive a {@link #onStartCommand(Intent, int, int)} |
kopriva | 762a08f | 2018-10-03 16:09:03 -0700 | [diff] [blame] | 419 | * call with a null Intent because it will only be restarted if |
Dianne Hackborn | 29e4a3c | 2009-09-30 22:35:40 -0700 | [diff] [blame] | 420 | * it is not finished processing all Intents sent to it (and any such |
| 421 | * pending events will be delivered at the point of restart). |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 422 | */ |
| 423 | public static final int START_REDELIVER_INTENT = 3; |
Dianne Hackborn | 67324c9 | 2016-04-18 13:55:25 -0700 | [diff] [blame] | 424 | |
| 425 | /** @hide */ |
Jeff Sharkey | 6503bd8 | 2017-04-19 23:24:18 -0600 | [diff] [blame] | 426 | @IntDef(flag = false, prefix = { "START_" }, value = { |
| 427 | START_STICKY_COMPATIBILITY, |
| 428 | START_STICKY, |
| 429 | START_NOT_STICKY, |
| 430 | START_REDELIVER_INTENT, |
| 431 | }) |
Dianne Hackborn | 67324c9 | 2016-04-18 13:55:25 -0700 | [diff] [blame] | 432 | @Retention(RetentionPolicy.SOURCE) |
| 433 | public @interface StartResult {} |
| 434 | |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 435 | /** |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 436 | * Special constant for reporting that we are done processing |
| 437 | * {@link #onTaskRemoved(Intent)}. |
| 438 | * @hide |
| 439 | */ |
| 440 | public static final int START_TASK_REMOVED_COMPLETE = 1000; |
| 441 | |
| 442 | /** |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 443 | * This flag is set in {@link #onStartCommand} if the Intent is a |
| 444 | * re-delivery of a previously delivered intent, because the service |
| 445 | * had previously returned {@link #START_REDELIVER_INTENT} but had been |
| 446 | * killed before calling {@link #stopSelf(int)} for that Intent. |
| 447 | */ |
| 448 | public static final int START_FLAG_REDELIVERY = 0x0001; |
| 449 | |
| 450 | /** |
| 451 | * This flag is set in {@link #onStartCommand} if the Intent is a |
Evan Charlton | b8d0717 | 2014-07-31 09:09:54 -0700 | [diff] [blame] | 452 | * retry because the original attempt never got to or returned from |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 453 | * {@link #onStartCommand(Intent, int, int)}. |
| 454 | */ |
| 455 | public static final int START_FLAG_RETRY = 0x0002; |
Dianne Hackborn | 67324c9 | 2016-04-18 13:55:25 -0700 | [diff] [blame] | 456 | |
| 457 | /** @hide */ |
Jeff Sharkey | 6503bd8 | 2017-04-19 23:24:18 -0600 | [diff] [blame] | 458 | @IntDef(flag = true, prefix = { "START_FLAG_" }, value = { |
| 459 | START_FLAG_REDELIVERY, |
| 460 | START_FLAG_RETRY, |
| 461 | }) |
Dianne Hackborn | 67324c9 | 2016-04-18 13:55:25 -0700 | [diff] [blame] | 462 | @Retention(RetentionPolicy.SOURCE) |
| 463 | public @interface StartArgFlags {} |
| 464 | |
| 465 | |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 466 | /** |
| 467 | * Called by the system every time a client explicitly starts the service by calling |
| 468 | * {@link android.content.Context#startService}, providing the arguments it supplied and a |
| 469 | * unique integer token representing the start request. Do not call this method directly. |
| 470 | * |
| 471 | * <p>For backwards compatibility, the default implementation calls |
| 472 | * {@link #onStart} and returns either {@link #START_STICKY} |
| 473 | * or {@link #START_STICKY_COMPATIBILITY}. |
| 474 | * |
Brad Fitzpatrick | 0166c35 | 2010-07-27 14:37:02 -0700 | [diff] [blame] | 475 | * <p class="caution">Note that the system calls this on your |
| 476 | * service's main thread. A service's main thread is the same |
Brad Fitzpatrick | ee34a49 | 2010-08-02 07:54:18 -0700 | [diff] [blame] | 477 | * thread where UI operations take place for Activities running in the |
Brad Fitzpatrick | 0166c35 | 2010-07-27 14:37:02 -0700 | [diff] [blame] | 478 | * same process. You should always avoid stalling the main |
| 479 | * thread's event loop. When doing long-running operations, |
| 480 | * network calls, or heavy disk I/O, you should kick off a new |
| 481 | * thread, or use {@link android.os.AsyncTask}.</p> |
| 482 | * |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 483 | * @param intent The Intent supplied to {@link android.content.Context#startService}, |
| 484 | * as given. This may be null if the service is being restarted after |
| 485 | * its process has gone away, and it had previously returned anything |
| 486 | * except {@link #START_STICKY_COMPATIBILITY}. |
Jeff Sharkey | 6503bd8 | 2017-04-19 23:24:18 -0600 | [diff] [blame] | 487 | * @param flags Additional data about this start request. |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 488 | * @param startId A unique integer representing this specific request to |
| 489 | * start. Use with {@link #stopSelfResult(int)}. |
| 490 | * |
| 491 | * @return The return value indicates what semantics the system should |
| 492 | * use for the service's current started state. It may be one of the |
| 493 | * constants associated with the {@link #START_CONTINUATION_MASK} bits. |
| 494 | * |
| 495 | * @see #stopSelfResult(int) |
| 496 | */ |
Dianne Hackborn | 67324c9 | 2016-04-18 13:55:25 -0700 | [diff] [blame] | 497 | public @StartResult int onStartCommand(Intent intent, @StartArgFlags int flags, int startId) { |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 498 | onStart(intent, startId); |
| 499 | return mStartCompatibility ? START_STICKY_COMPATIBILITY : START_STICKY; |
| 500 | } |
| 501 | |
| 502 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 503 | * Called by the system to notify a Service that it is no longer used and is being removed. The |
Ken Wakasa | f76a50c | 2012-03-09 19:56:35 +0900 | [diff] [blame] | 504 | * service should clean up any resources it holds (threads, registered |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 505 | * receivers, etc) at this point. Upon return, there will be no more calls |
| 506 | * in to this Service object and it is effectively dead. Do not call this method directly. |
| 507 | */ |
| 508 | public void onDestroy() { |
| 509 | } |
| 510 | |
| 511 | public void onConfigurationChanged(Configuration newConfig) { |
| 512 | } |
| 513 | |
| 514 | public void onLowMemory() { |
| 515 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 516 | |
| 517 | public void onTrimMemory(int level) { |
| 518 | } |
| 519 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 520 | /** |
| 521 | * Return the communication channel to the service. May return null if |
| 522 | * clients can not bind to the service. The returned |
| 523 | * {@link android.os.IBinder} is usually for a complex interface |
Scott Main | 40eee61 | 2012-08-06 17:48:37 -0700 | [diff] [blame] | 524 | * that has been <a href="{@docRoot}guide/components/aidl.html">described using |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 525 | * aidl</a>. |
| 526 | * |
| 527 | * <p><em>Note that unlike other application components, calls on to the |
| 528 | * IBinder interface returned here may not happen on the main thread |
Scott Main | 7aee61f | 2011-02-08 11:25:01 -0800 | [diff] [blame] | 529 | * of the process</em>. More information about the main thread can be found in |
| 530 | * <a href="{@docRoot}guide/topics/fundamentals/processes-and-threads.html">Processes and |
| 531 | * Threads</a>.</p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 532 | * |
| 533 | * @param intent The Intent that was used to bind to this service, |
| 534 | * as given to {@link android.content.Context#bindService |
| 535 | * Context.bindService}. Note that any extras that were included with |
| 536 | * the Intent at that point will <em>not</em> be seen here. |
| 537 | * |
| 538 | * @return Return an IBinder through which clients can call on to the |
| 539 | * service. |
| 540 | */ |
Scott Kennedy | 7db73ed | 2015-02-19 15:31:11 -0800 | [diff] [blame] | 541 | @Nullable |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 542 | public abstract IBinder onBind(Intent intent); |
| 543 | |
| 544 | /** |
| 545 | * Called when all clients have disconnected from a particular interface |
| 546 | * published by the service. The default implementation does nothing and |
| 547 | * returns false. |
| 548 | * |
| 549 | * @param intent The Intent that was used to bind to this service, |
| 550 | * as given to {@link android.content.Context#bindService |
| 551 | * Context.bindService}. Note that any extras that were included with |
| 552 | * the Intent at that point will <em>not</em> be seen here. |
| 553 | * |
| 554 | * @return Return true if you would like to have the service's |
| 555 | * {@link #onRebind} method later called when new clients bind to it. |
| 556 | */ |
| 557 | public boolean onUnbind(Intent intent) { |
| 558 | return false; |
| 559 | } |
| 560 | |
| 561 | /** |
| 562 | * Called when new clients have connected to the service, after it had |
| 563 | * previously been notified that all had disconnected in its |
| 564 | * {@link #onUnbind}. This will only be called if the implementation |
| 565 | * of {@link #onUnbind} was overridden to return true. |
| 566 | * |
| 567 | * @param intent The Intent that was used to bind to this service, |
| 568 | * as given to {@link android.content.Context#bindService |
| 569 | * Context.bindService}. Note that any extras that were included with |
| 570 | * the Intent at that point will <em>not</em> be seen here. |
| 571 | */ |
| 572 | public void onRebind(Intent intent) { |
| 573 | } |
| 574 | |
| 575 | /** |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 576 | * This is called if the service is currently running and the user has |
| 577 | * removed a task that comes from the service's application. If you have |
| 578 | * set {@link android.content.pm.ServiceInfo#FLAG_STOP_WITH_TASK ServiceInfo.FLAG_STOP_WITH_TASK} |
| 579 | * then you will not receive this callback; instead, the service will simply |
| 580 | * be stopped. |
| 581 | * |
| 582 | * @param rootIntent The original root Intent that was used to launch |
| 583 | * the task that is being removed. |
| 584 | */ |
| 585 | public void onTaskRemoved(Intent rootIntent) { |
| 586 | } |
| 587 | |
| 588 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 589 | * Stop the service, if it was previously started. This is the same as |
| 590 | * calling {@link android.content.Context#stopService} for this particular service. |
| 591 | * |
| 592 | * @see #stopSelfResult(int) |
| 593 | */ |
| 594 | public final void stopSelf() { |
| 595 | stopSelf(-1); |
| 596 | } |
| 597 | |
| 598 | /** |
| 599 | * Old version of {@link #stopSelfResult} that doesn't return a result. |
| 600 | * |
| 601 | * @see #stopSelfResult |
| 602 | */ |
| 603 | public final void stopSelf(int startId) { |
| 604 | if (mActivityManager == null) { |
| 605 | return; |
| 606 | } |
| 607 | try { |
| 608 | mActivityManager.stopServiceToken( |
| 609 | new ComponentName(this, mClassName), mToken, startId); |
| 610 | } catch (RemoteException ex) { |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | /** |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 615 | * Stop the service if the most recent time it was started was |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 616 | * <var>startId</var>. This is the same as calling {@link |
| 617 | * android.content.Context#stopService} for this particular service but allows you to |
| 618 | * safely avoid stopping if there is a start request from a client that you |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 619 | * haven't yet seen in {@link #onStart}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 620 | * |
Dianne Hackborn | 29e4a3c | 2009-09-30 22:35:40 -0700 | [diff] [blame] | 621 | * <p><em>Be careful about ordering of your calls to this function.</em>. |
| 622 | * If you call this function with the most-recently received ID before |
| 623 | * you have called it for previously received IDs, the service will be |
| 624 | * immediately stopped anyway. If you may end up processing IDs out |
| 625 | * of order (such as by dispatching them on separate threads), then you |
| 626 | * are responsible for stopping them in the same order you received them.</p> |
| 627 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 628 | * @param startId The most recent start identifier received in {@link |
| 629 | * #onStart}. |
| 630 | * @return Returns true if the startId matches the last start request |
| 631 | * and the service will be stopped, else false. |
| 632 | * |
| 633 | * @see #stopSelf() |
| 634 | */ |
| 635 | public final boolean stopSelfResult(int startId) { |
| 636 | if (mActivityManager == null) { |
| 637 | return false; |
| 638 | } |
| 639 | try { |
| 640 | return mActivityManager.stopServiceToken( |
| 641 | new ComponentName(this, mClassName), mToken, startId); |
| 642 | } catch (RemoteException ex) { |
| 643 | } |
| 644 | return false; |
| 645 | } |
| 646 | |
| 647 | /** |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 648 | * @deprecated This is a now a no-op, use |
Dianne Hackborn | 29e4a3c | 2009-09-30 22:35:40 -0700 | [diff] [blame] | 649 | * {@link #startForeground(int, Notification)} instead. This method |
| 650 | * has been turned into a no-op rather than simply being deprecated |
| 651 | * because analysis of numerous poorly behaving devices has shown that |
| 652 | * increasingly often the trouble is being caused in part by applications |
| 653 | * that are abusing it. Thus, given a choice between introducing |
| 654 | * problems in existing applications using this API (by allowing them to |
| 655 | * be killed when they would like to avoid it), vs allowing the performance |
| 656 | * of the entire system to be decreased, this method was deemed less |
| 657 | * important. |
Dianne Hackborn | 4f3867e | 2010-12-14 22:09:51 -0800 | [diff] [blame] | 658 | * |
| 659 | * @hide |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 660 | */ |
| 661 | @Deprecated |
Mathew Inwood | 4fb17d1 | 2018-08-14 14:25:44 +0100 | [diff] [blame] | 662 | @UnsupportedAppUsage |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 663 | public final void setForeground(boolean isForeground) { |
| 664 | Log.w(TAG, "setForeground: ignoring old API call on " + getClass().getName()); |
| 665 | } |
| 666 | |
| 667 | /** |
Dianne Hackborn | 4ae51e0 | 2017-05-04 16:53:11 -0700 | [diff] [blame] | 668 | * If your service is started (running through {@link Context#startService(Intent)}), then |
| 669 | * also make this service run in the foreground, supplying the ongoing |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 670 | * notification to be shown to the user while in this state. |
Dianne Hackborn | 4ae51e0 | 2017-05-04 16:53:11 -0700 | [diff] [blame] | 671 | * By default started services are background, meaning that their process won't be given |
| 672 | * foreground CPU scheduling (unless something else in that process is foreground) and, |
| 673 | * if the system needs to kill them to reclaim more memory (such as to display a large page in a |
| 674 | * web browser), they can be killed without too much harm. You use |
| 675 | * {@link #startForeground} if killing your service would be disruptive to the user, such as |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 676 | * if your service is performing background music playback, so the user |
| 677 | * would notice if their music stopped playing. |
Dianne Hackborn | 4ae51e0 | 2017-05-04 16:53:11 -0700 | [diff] [blame] | 678 | * |
| 679 | * <p>Note that calling this method does <em>not</em> put the service in the started state |
| 680 | * itself, even though the name sounds like it. You must always call |
| 681 | * {@link #startService(Intent)} first to tell the system it should keep the service running, |
| 682 | * and then use this method to tell it to keep it running harder.</p> |
| 683 | * |
Dianne Hackborn | dd027b3 | 2018-01-19 17:44:46 -0800 | [diff] [blame] | 684 | * <p>Apps targeting API {@link android.os.Build.VERSION_CODES#P} or later must request |
| 685 | * the permission {@link android.Manifest.permission#FOREGROUND_SERVICE} in order to use |
| 686 | * this API.</p> |
| 687 | * |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 688 | * @param id The identifier for this notification as per |
| 689 | * {@link NotificationManager#notify(int, Notification) |
Scott Main | 2e6de8f | 2013-04-30 10:51:12 -0700 | [diff] [blame] | 690 | * NotificationManager.notify(int, Notification)}; must not be 0. |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 691 | * @param notification The Notification to be displayed. |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 692 | * |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 693 | * @see #stopForeground(boolean) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 694 | */ |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 695 | public final void startForeground(int id, Notification notification) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 696 | try { |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 697 | mActivityManager.setServiceForeground( |
| 698 | new ComponentName(this, mClassName), mToken, id, |
| 699 | notification, 0); |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 700 | } catch (RemoteException ex) { |
| 701 | } |
| 702 | } |
| 703 | |
| 704 | /** |
Dianne Hackborn | 67324c9 | 2016-04-18 13:55:25 -0700 | [diff] [blame] | 705 | * Synonym for {@link #stopForeground(int)}. |
| 706 | * @param removeNotification If true, the {@link #STOP_FOREGROUND_REMOVE} flag |
| 707 | * will be supplied. |
| 708 | * @see #stopForeground(int) |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 709 | * @see #startForeground(int, Notification) |
| 710 | */ |
| 711 | public final void stopForeground(boolean removeNotification) { |
Dianne Hackborn | 67324c9 | 2016-04-18 13:55:25 -0700 | [diff] [blame] | 712 | stopForeground(removeNotification ? STOP_FOREGROUND_REMOVE : 0); |
| 713 | } |
| 714 | |
| 715 | /** |
| 716 | * Remove this service from foreground state, allowing it to be killed if |
Dianne Hackborn | 4ae51e0 | 2017-05-04 16:53:11 -0700 | [diff] [blame] | 717 | * more memory is needed. This does not stop the service from running (for that |
| 718 | * you use {@link #stopSelf()} or related methods), just takes it out of the |
| 719 | * foreground state. |
Jeff Sharkey | 6503bd8 | 2017-04-19 23:24:18 -0600 | [diff] [blame] | 720 | * |
| 721 | * @param flags additional behavior options. |
Dianne Hackborn | 67324c9 | 2016-04-18 13:55:25 -0700 | [diff] [blame] | 722 | * @see #startForeground(int, Notification) |
| 723 | */ |
| 724 | public final void stopForeground(@StopForegroundFlags int flags) { |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 725 | try { |
| 726 | mActivityManager.setServiceForeground( |
Dianne Hackborn | 67324c9 | 2016-04-18 13:55:25 -0700 | [diff] [blame] | 727 | new ComponentName(this, mClassName), mToken, 0, null, flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 728 | } catch (RemoteException ex) { |
| 729 | } |
| 730 | } |
Dianne Hackborn | 67324c9 | 2016-04-18 13:55:25 -0700 | [diff] [blame] | 731 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 732 | /** |
| 733 | * Print the Service's state into the given stream. This gets invoked if |
kmccormick | f6dc107 | 2013-03-29 16:00:50 -0700 | [diff] [blame] | 734 | * you run "adb shell dumpsys activity service <yourservicename>" |
| 735 | * (note that for this command to work, the service must be running, and |
| 736 | * you must specify a fully-qualified service name). |
Jeff Sharkey | 5554b70 | 2012-04-11 18:30:51 -0700 | [diff] [blame] | 737 | * This is distinct from "dumpsys <servicename>", which only works for |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 738 | * named system services and which invokes the {@link IBinder#dump} method |
| 739 | * on the {@link IBinder} interface registered with ServiceManager. |
| 740 | * |
| 741 | * @param fd The raw file descriptor that the dump is being sent to. |
| 742 | * @param writer The PrintWriter to which you should dump your state. This will be |
| 743 | * closed for you after you return. |
| 744 | * @param args additional arguments to the dump request. |
| 745 | */ |
| 746 | protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) { |
| 747 | writer.println("nothing to dump"); |
| 748 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 749 | |
| 750 | // ------------------ Internal API ------------------ |
| 751 | |
| 752 | /** |
| 753 | * @hide |
| 754 | */ |
Mathew Inwood | 4fb17d1 | 2018-08-14 14:25:44 +0100 | [diff] [blame] | 755 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 756 | public final void attach( |
| 757 | Context context, |
| 758 | ActivityThread thread, String className, IBinder token, |
| 759 | Application application, Object activityManager) { |
| 760 | attachBaseContext(context); |
| 761 | mThread = thread; // NOTE: unused - remove? |
| 762 | mClassName = className; |
| 763 | mToken = token; |
| 764 | mApplication = application; |
| 765 | mActivityManager = (IActivityManager)activityManager; |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 766 | mStartCompatibility = getApplicationInfo().targetSdkVersion |
| 767 | < Build.VERSION_CODES.ECLAIR; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 768 | } |
Amith Yamasani | 7592825 | 2017-05-22 15:10:47 -0700 | [diff] [blame] | 769 | |
| 770 | /** |
| 771 | * @hide |
| 772 | * Clean up any references to avoid leaks. |
| 773 | */ |
| 774 | public final void detachAndCleanUp() { |
| 775 | mToken = null; |
| 776 | } |
| 777 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 778 | final String getClassName() { |
| 779 | return mClassName; |
| 780 | } |
| 781 | |
| 782 | // set by the thread after the constructor and before onCreate(Bundle icicle) is called. |
Mathew Inwood | 4fb17d1 | 2018-08-14 14:25:44 +0100 | [diff] [blame] | 783 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 784 | private ActivityThread mThread = null; |
Mathew Inwood | 4fb17d1 | 2018-08-14 14:25:44 +0100 | [diff] [blame] | 785 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 786 | private String mClassName = null; |
Mathew Inwood | 4fb17d1 | 2018-08-14 14:25:44 +0100 | [diff] [blame] | 787 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 788 | private IBinder mToken = null; |
Mathew Inwood | 4fb17d1 | 2018-08-14 14:25:44 +0100 | [diff] [blame] | 789 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 790 | private Application mApplication = null; |
Mathew Inwood | 4fb17d1 | 2018-08-14 14:25:44 +0100 | [diff] [blame] | 791 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 792 | private IActivityManager mActivityManager = null; |
Mathew Inwood | 4fb17d1 | 2018-08-14 14:25:44 +0100 | [diff] [blame] | 793 | @UnsupportedAppUsage |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 794 | private boolean mStartCompatibility = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 795 | } |