blob: e4a22c42d9bcbeadeb63d1ea6a57adca35cdf024 [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
Tor Norbye83c68962015-03-10 20:55:31 -070019import android.annotation.WorkerThread;
Ryan Lothiance8d4f52016-01-21 15:07:01 +000020import android.annotation.Nullable;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import android.content.Intent;
22import android.os.Handler;
23import android.os.HandlerThread;
24import android.os.IBinder;
25import android.os.Looper;
26import android.os.Message;
27
28/**
Dan Egnor6e3d9882010-04-06 18:28:42 -070029 * IntentService is a base class for {@link Service}s that handle asynchronous
30 * requests (expressed as {@link Intent}s) on demand. Clients send requests
Wu-cheng Licae57d62010-04-07 13:52:37 +080031 * through {@link android.content.Context#startService(Intent)} calls; the
32 * service is started as needed, handles each Intent in turn using a worker
33 * thread, and stops itself when it runs out of work.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034 *
Dan Egnor6e3d9882010-04-06 18:28:42 -070035 * <p>This "work queue processor" pattern is commonly used to offload tasks
36 * from an application's main thread. The IntentService class exists to
37 * simplify this pattern and take care of the mechanics. To use it, extend
38 * IntentService and implement {@link #onHandleIntent(Intent)}. IntentService
39 * will receive the Intents, launch a worker thread, and stop the service as
40 * appropriate.
41 *
42 * <p>All requests are handled on a single worker thread -- they may take as
43 * long as necessary (and will not block the application's main loop), but
44 * only one request will be processed at a time.
45 *
Joe Fernandezb54e7a32011-10-03 15:09:50 -070046 * <div class="special reference">
47 * <h3>Developer Guides</h3>
48 * <p>For a detailed discussion about how to create services, read the
Hemal Patel19182142016-09-30 14:59:23 -070049 * <a href="{@docRoot}guide/components/services.html">Services</a> developer
50 * guide.</p>
Joe Fernandezb54e7a32011-10-03 15:09:50 -070051 * </div>
52 *
Dan Egnor6e3d9882010-04-06 18:28:42 -070053 * @see android.os.AsyncTask
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054 */
55public abstract class IntentService extends Service {
56 private volatile Looper mServiceLooper;
57 private volatile ServiceHandler mServiceHandler;
58 private String mName;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -070059 private boolean mRedelivery;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060
61 private final class ServiceHandler extends Handler {
62 public ServiceHandler(Looper looper) {
63 super(looper);
64 }
65
66 @Override
67 public void handleMessage(Message msg) {
68 onHandleIntent((Intent)msg.obj);
69 stopSelf(msg.arg1);
70 }
71 }
72
Dan Egnor6e3d9882010-04-06 18:28:42 -070073 /**
74 * Creates an IntentService. Invoked by your subclass's constructor.
75 *
76 * @param name Used to name the worker thread, important only for debugging.
77 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078 public IntentService(String name) {
79 super();
80 mName = name;
81 }
82
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -070083 /**
Dan Egnor6e3d9882010-04-06 18:28:42 -070084 * Sets intent redelivery preferences. Usually called from the constructor
85 * with your preferred semantics.
86 *
87 * <p>If enabled is true,
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -070088 * {@link #onStartCommand(Intent, int, int)} will return
Dan Egnor6e3d9882010-04-06 18:28:42 -070089 * {@link Service#START_REDELIVER_INTENT}, so if this process dies before
90 * {@link #onHandleIntent(Intent)} returns, the process will be restarted
91 * and the intent redelivered. If multiple Intents have been sent, only
92 * the most recent one is guaranteed to be redelivered.
93 *
94 * <p>If enabled is false (the default),
95 * {@link #onStartCommand(Intent, int, int)} will return
96 * {@link Service#START_NOT_STICKY}, and if the process dies, the Intent
97 * dies along with it.
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -070098 */
99 public void setIntentRedelivery(boolean enabled) {
100 mRedelivery = enabled;
101 }
Dan Egnor6e3d9882010-04-06 18:28:42 -0700102
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103 @Override
104 public void onCreate() {
Dan Egnor6e3d9882010-04-06 18:28:42 -0700105 // TODO: It would be nice to have an option to hold a partial wakelock
106 // during processing, and to have a static startService(Context, Intent)
107 // method that would launch the service & hand off a wakelock.
108
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109 super.onCreate();
110 HandlerThread thread = new HandlerThread("IntentService[" + mName + "]");
111 thread.start();
112
113 mServiceLooper = thread.getLooper();
114 mServiceHandler = new ServiceHandler(mServiceLooper);
115 }
116
117 @Override
Ryan Lothiance8d4f52016-01-21 15:07:01 +0000118 public void onStart(@Nullable Intent intent, int startId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119 Message msg = mServiceHandler.obtainMessage();
120 msg.arg1 = startId;
121 msg.obj = intent;
122 mServiceHandler.sendMessage(msg);
123 }
124
Scott Main7a14b932010-10-04 18:30:46 -0700125 /**
126 * You should not override this method for your IntentService. Instead,
127 * override {@link #onHandleIntent}, which the system calls when the IntentService
128 * receives a start request.
129 * @see android.app.Service#onStartCommand
130 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131 @Override
Ryan Lothiance8d4f52016-01-21 15:07:01 +0000132 public int onStartCommand(@Nullable Intent intent, int flags, int startId) {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700133 onStart(intent, startId);
134 return mRedelivery ? START_REDELIVER_INTENT : START_NOT_STICKY;
135 }
Dan Egnor6e3d9882010-04-06 18:28:42 -0700136
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700137 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 public void onDestroy() {
139 mServiceLooper.quit();
140 }
141
Scott Main7a14b932010-10-04 18:30:46 -0700142 /**
143 * Unless you provide binding for your service, you don't need to implement this
Ryan Lothiance8d4f52016-01-21 15:07:01 +0000144 * method, because the default implementation returns null.
Scott Main7a14b932010-10-04 18:30:46 -0700145 * @see android.app.Service#onBind
146 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147 @Override
Ryan Lothiance8d4f52016-01-21 15:07:01 +0000148 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 public IBinder onBind(Intent intent) {
150 return null;
151 }
152
153 /**
Dan Egnor6e3d9882010-04-06 18:28:42 -0700154 * This method is invoked on the worker thread with a request to process.
155 * Only one Intent is processed at a time, but the processing happens on a
156 * worker thread that runs independently from other application logic.
157 * So, if this code takes a long time, it will hold up other requests to
158 * the same IntentService, but it will not hold up anything else.
Scott Main7a14b932010-10-04 18:30:46 -0700159 * When all requests have been handled, the IntentService stops itself,
160 * so you should not call {@link #stopSelf}.
Dan Egnor6e3d9882010-04-06 18:28:42 -0700161 *
Wu-cheng Licae57d62010-04-07 13:52:37 +0800162 * @param intent The value passed to {@link
163 * android.content.Context#startService(Intent)}.
Ryan Lothiance8d4f52016-01-21 15:07:01 +0000164 * This may be null if the service is being restarted after
165 * its process has gone away; see
166 * {@link android.app.Service#onStartCommand}
167 * for details.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168 */
Tor Norbye83c68962015-03-10 20:55:31 -0700169 @WorkerThread
Ryan Lothiance8d4f52016-01-21 15:07:01 +0000170 protected abstract void onHandleIntent(@Nullable Intent intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171}