blob: 74fb99a0909f390dde77c7d054b4098755adfd50 [file] [log] [blame]
Kenny Root15a4d2f2010-03-11 18:20:12 -08001/*
2 * Copyright (C) 2008 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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017package android.app;
18
Mathew Inwood4fb17d12018-08-14 14:25:44 +010019import android.annotation.UnsupportedAppUsage;
Tor Norbye83c68962015-03-10 20:55:31 -070020import android.annotation.WorkerThread;
Ryan Lothiance8d4f52016-01-21 15:07:01 +000021import android.annotation.Nullable;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.content.Intent;
23import android.os.Handler;
24import android.os.HandlerThread;
25import android.os.IBinder;
26import android.os.Looper;
27import android.os.Message;
28
29/**
Dan Egnor6e3d9882010-04-06 18:28:42 -070030 * IntentService is a base class for {@link Service}s that handle asynchronous
31 * requests (expressed as {@link Intent}s) on demand. Clients send requests
Wu-cheng Licae57d62010-04-07 13:52:37 +080032 * through {@link android.content.Context#startService(Intent)} calls; the
33 * service is started as needed, handles each Intent in turn using a worker
34 * thread, and stops itself when it runs out of work.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035 *
Dan Egnor6e3d9882010-04-06 18:28:42 -070036 * <p>This "work queue processor" pattern is commonly used to offload tasks
37 * from an application's main thread. The IntentService class exists to
38 * simplify this pattern and take care of the mechanics. To use it, extend
39 * IntentService and implement {@link #onHandleIntent(Intent)}. IntentService
40 * will receive the Intents, launch a worker thread, and stop the service as
41 * appropriate.
42 *
43 * <p>All requests are handled on a single worker thread -- they may take as
44 * long as necessary (and will not block the application's main loop), but
45 * only one request will be processed at a time.
46 *
Joe Fernandezb54e7a32011-10-03 15:09:50 -070047 * <div class="special reference">
48 * <h3>Developer Guides</h3>
49 * <p>For a detailed discussion about how to create services, read the
Hemal Patel19182142016-09-30 14:59:23 -070050 * <a href="{@docRoot}guide/components/services.html">Services</a> developer
51 * guide.</p>
Joe Fernandezb54e7a32011-10-03 15:09:50 -070052 * </div>
53 *
Andrew Solovay494f1a42017-07-31 19:13:38 -070054 * @see android.support.v4.app.JobIntentService
Charles Munger6f8b0902019-11-12 08:35:39 -080055 *
56 * @deprecated IntentService is subject to all the
57 * <a href="/preview/features/background.html">background execution limits</a>
58 * imposed with Android 8.0 (API level 26). Consider using {@link androidx.work.WorkManager}
59 * or {@link androidx.core.app.JobIntentService}, which uses jobs
60 * instead of services when running on Android 8.0 or higher.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061 */
Charles Munger6f8b0902019-11-12 08:35:39 -080062@Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063public abstract class IntentService extends Service {
64 private volatile Looper mServiceLooper;
Mathew Inwood4fb17d12018-08-14 14:25:44 +010065 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066 private volatile ServiceHandler mServiceHandler;
67 private String mName;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -070068 private boolean mRedelivery;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069
70 private final class ServiceHandler extends Handler {
71 public ServiceHandler(Looper looper) {
72 super(looper);
73 }
74
75 @Override
76 public void handleMessage(Message msg) {
77 onHandleIntent((Intent)msg.obj);
78 stopSelf(msg.arg1);
79 }
80 }
81
Dan Egnor6e3d9882010-04-06 18:28:42 -070082 /**
83 * Creates an IntentService. Invoked by your subclass's constructor.
84 *
85 * @param name Used to name the worker thread, important only for debugging.
86 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087 public IntentService(String name) {
88 super();
89 mName = name;
90 }
91
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -070092 /**
Dan Egnor6e3d9882010-04-06 18:28:42 -070093 * Sets intent redelivery preferences. Usually called from the constructor
94 * with your preferred semantics.
95 *
96 * <p>If enabled is true,
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -070097 * {@link #onStartCommand(Intent, int, int)} will return
Dan Egnor6e3d9882010-04-06 18:28:42 -070098 * {@link Service#START_REDELIVER_INTENT}, so if this process dies before
99 * {@link #onHandleIntent(Intent)} returns, the process will be restarted
100 * and the intent redelivered. If multiple Intents have been sent, only
101 * the most recent one is guaranteed to be redelivered.
102 *
103 * <p>If enabled is false (the default),
104 * {@link #onStartCommand(Intent, int, int)} will return
105 * {@link Service#START_NOT_STICKY}, and if the process dies, the Intent
106 * dies along with it.
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700107 */
108 public void setIntentRedelivery(boolean enabled) {
109 mRedelivery = enabled;
110 }
Dan Egnor6e3d9882010-04-06 18:28:42 -0700111
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112 @Override
113 public void onCreate() {
Dan Egnor6e3d9882010-04-06 18:28:42 -0700114 // TODO: It would be nice to have an option to hold a partial wakelock
115 // during processing, and to have a static startService(Context, Intent)
116 // method that would launch the service & hand off a wakelock.
117
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 super.onCreate();
119 HandlerThread thread = new HandlerThread("IntentService[" + mName + "]");
120 thread.start();
121
122 mServiceLooper = thread.getLooper();
123 mServiceHandler = new ServiceHandler(mServiceLooper);
124 }
125
126 @Override
Ryan Lothiance8d4f52016-01-21 15:07:01 +0000127 public void onStart(@Nullable Intent intent, int startId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128 Message msg = mServiceHandler.obtainMessage();
129 msg.arg1 = startId;
130 msg.obj = intent;
131 mServiceHandler.sendMessage(msg);
132 }
133
Scott Main7a14b932010-10-04 18:30:46 -0700134 /**
135 * You should not override this method for your IntentService. Instead,
136 * override {@link #onHandleIntent}, which the system calls when the IntentService
137 * receives a start request.
138 * @see android.app.Service#onStartCommand
139 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 @Override
Ryan Lothiance8d4f52016-01-21 15:07:01 +0000141 public int onStartCommand(@Nullable Intent intent, int flags, int startId) {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700142 onStart(intent, startId);
143 return mRedelivery ? START_REDELIVER_INTENT : START_NOT_STICKY;
144 }
Dan Egnor6e3d9882010-04-06 18:28:42 -0700145
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700146 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147 public void onDestroy() {
148 mServiceLooper.quit();
149 }
150
Scott Main7a14b932010-10-04 18:30:46 -0700151 /**
152 * Unless you provide binding for your service, you don't need to implement this
Ryan Lothiance8d4f52016-01-21 15:07:01 +0000153 * method, because the default implementation returns null.
Scott Main7a14b932010-10-04 18:30:46 -0700154 * @see android.app.Service#onBind
155 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156 @Override
Ryan Lothiance8d4f52016-01-21 15:07:01 +0000157 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158 public IBinder onBind(Intent intent) {
159 return null;
160 }
161
162 /**
Dan Egnor6e3d9882010-04-06 18:28:42 -0700163 * This method is invoked on the worker thread with a request to process.
164 * Only one Intent is processed at a time, but the processing happens on a
165 * worker thread that runs independently from other application logic.
166 * So, if this code takes a long time, it will hold up other requests to
167 * the same IntentService, but it will not hold up anything else.
Scott Main7a14b932010-10-04 18:30:46 -0700168 * When all requests have been handled, the IntentService stops itself,
169 * so you should not call {@link #stopSelf}.
Dan Egnor6e3d9882010-04-06 18:28:42 -0700170 *
Wu-cheng Licae57d62010-04-07 13:52:37 +0800171 * @param intent The value passed to {@link
172 * android.content.Context#startService(Intent)}.
Ryan Lothiance8d4f52016-01-21 15:07:01 +0000173 * This may be null if the service is being restarted after
174 * its process has gone away; see
175 * {@link android.app.Service#onStartCommand}
176 * for details.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 */
Tor Norbye83c68962015-03-10 20:55:31 -0700178 @WorkerThread
Ryan Lothiance8d4f52016-01-21 15:07:01 +0000179 protected abstract void onHandleIntent(@Nullable Intent intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180}