IntentService
public
abstract
class
IntentService
extends Service
java.lang.Object | ||||
↳ | android.content.Context | |||
↳ | android.content.ContextWrapper | |||
↳ | android.app.Service | |||
↳ | android.app.IntentService |
IntentService is a base class for Service
s that handle asynchronous
requests (expressed as Intent
s) on demand. Clients send requests
through Context.startService(Intent)
calls; the
service is started as needed, handles each Intent in turn using a worker
thread, and stops itself when it runs out of work.
This "work queue processor" pattern is commonly used to offload tasks
from an application's main thread. The IntentService class exists to
simplify this pattern and take care of the mechanics. To use it, extend
IntentService and implement onHandleIntent(Intent)
. IntentService
will receive the Intents, launch a worker thread, and stop the service as
appropriate.
All requests are handled on a single worker thread -- they may take as long as necessary (and will not block the application's main loop), but only one request will be processed at a time.
Note: IntentService is subject to all the
background execution limits
imposed with Android 8.0 (API level 26). In most cases, you are better off
using JobIntentService
, which uses jobs
instead of services when running on Android 8.0 or higher.
Developer Guides
For a detailed discussion about how to create services, read the Services developer guide.
See also:
Summary
Inherited constants | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
From class
android.app.Service
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
From class
android.content.Context
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
From interface
android.content.ComponentCallbacks2
|
Public constructors | |
---|---|
IntentService(String name)
Creates an IntentService. |
Public methods | |
---|---|
IBinder
|
onBind(Intent intent)
Unless you provide binding for your service, you don't need to implement this method, because the default implementation returns null. |
void
|
onCreate()
Called by the system when the service is first created. |
void
|
onDestroy()
Called by the system to notify a Service that it is no longer used and is being removed. |
void
|
onStart(Intent intent, int startId)
This method is deprecated.
Implement |
int
|
onStartCommand(Intent intent, int flags, int startId)
You should not override this method for your IntentService. |
void
|
setIntentRedelivery(boolean enabled)
Sets intent redelivery preferences. |
Protected methods | |
---|---|
abstract
void
|
onHandleIntent(Intent intent)
This method is invoked on the worker thread with a request to process. |
Inherited methods | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
From class
android.app.Service
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
From class
android.content.ContextWrapper
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
From class
android.content.Context
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
From class
java.lang.Object
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
From interface
android.content.ComponentCallbacks2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
From interface
android.content.ComponentCallbacks
|
Public constructors
IntentService
public IntentService (String name)
Creates an IntentService. Invoked by your subclass's constructor.
Parameters | |
---|---|
name |
String : Used to name the worker thread, important only for debugging.
|
Public methods
onBind
public IBinder onBind (Intent intent)
Unless you provide binding for your service, you don't need to implement this method, because the default implementation returns null.
Parameters | |
---|---|
intent |
Intent : The Intent that was used to bind to this service,
as given to Context.bindService . Note that any extras that were included with
the Intent at that point will not be seen here. |
Returns | |
---|---|
IBinder |
Return an IBinder through which clients can call on to the
service.
This value may be |
See also:
onCreate
public void onCreate ()
Called by the system when the service is first created. Do not call this method directly.
onDestroy
public void onDestroy ()
Called by the system to notify a Service that it is no longer used and is being removed. The service should clean up any resources it holds (threads, registered receivers, etc) at this point. Upon return, there will be no more calls in to this Service object and it is effectively dead. Do not call this method directly.
onStart
public void onStart (Intent intent, int startId)
This method is deprecated.
Implement onStartCommand(Intent, int, int)
instead.
Parameters | |
---|---|
intent |
Intent This value may be |
startId |
int |
onStartCommand
public int onStartCommand (Intent intent, int flags, int startId)
You should not override this method for your IntentService. Instead,
override onHandleIntent(Intent)
, which the system calls when the IntentService
receives a start request.
Parameters | |
---|---|
intent |
Intent : The Intent supplied to Context.startService(Intent) ,
as given. This may be null if the service is being restarted after
its process has gone away, and it had previously returned anything
except Service.START_STICKY_COMPATIBILITY . |
flags |
int : Additional data about this start request. |
startId |
int : A unique integer representing this specific request to
start. Use with Service.stopSelfResult(int) . |
Returns | |
---|---|
int |
The return value indicates what semantics the system should
use for the service's current started state. It may be one of the
constants associated with the START_CONTINUATION_MASK bits. |
See also:
setIntentRedelivery
public void setIntentRedelivery (boolean enabled)
Sets intent redelivery preferences. Usually called from the constructor with your preferred semantics.
If enabled is true,
onStartCommand(Intent, int, int)
will return
Service.START_REDELIVER_INTENT
, so if this process dies before
onHandleIntent(Intent)
returns, the process will be restarted
and the intent redelivered. If multiple Intents have been sent, only
the most recent one is guaranteed to be redelivered.
If enabled is false (the default),
onStartCommand(Intent, int, int)
will return
Service.START_NOT_STICKY
, and if the process dies, the Intent
dies along with it.
Parameters | |
---|---|
enabled |
boolean |
Protected methods
onHandleIntent
protected abstract void onHandleIntent (Intent intent)
This method is invoked on the worker thread with a request to process.
Only one Intent is processed at a time, but the processing happens on a
worker thread that runs independently from other application logic.
So, if this code takes a long time, it will hold up other requests to
the same IntentService, but it will not hold up anything else.
When all requests have been handled, the IntentService stops itself,
so you should not call Service.stopSelf()
.
This method may take several seconds to complete, so it should only be called from a worker thread.
Parameters | |
---|---|
intent |
Intent : The value passed to Context.startService(Intent) .
This may be null if the service is being restarted after
its process has gone away; see
Service.onStartCommand(Intent, int, int)
for details.
|
Interfaces
- ActionBar.OnMenuVisibilityListener
- ActionBar.OnNavigationListener
- ActionBar.TabListener
- AlarmManager.OnAlarmListener
- Application.ActivityLifecycleCallbacks
- Application.OnProvideAssistDataListener
- AppOpsManager.OnOpChangedListener
- DatePickerDialog.OnDateSetListener
- FragmentBreadCrumbs.OnBreadCrumbClickListener
- FragmentManager.BackStackEntry
- FragmentManager.OnBackStackChangedListener
- KeyguardManager.OnKeyguardExitResult
- LoaderManager.LoaderCallbacks
- Notification.Action.Extender
- Notification.Extender
- PendingIntent.OnFinished
- SearchManager.OnCancelListener
- SearchManager.OnDismissListener
- SharedElementCallback.OnSharedElementsReadyListener
- TimePickerDialog.OnTimeSetListener
- UiAutomation.AccessibilityEventFilter
- UiAutomation.OnAccessibilityEventListener
- WallpaperManager.OnColorsChangedListener
Classes
- ActionBar
- ActionBar.LayoutParams
- ActionBar.Tab
- Activity
- ActivityGroup
- ActivityManager
- ActivityManager.AppTask
- ActivityManager.MemoryInfo
- ActivityManager.ProcessErrorStateInfo
- ActivityManager.RecentTaskInfo
- ActivityManager.RunningAppProcessInfo
- ActivityManager.RunningServiceInfo
- ActivityManager.RunningTaskInfo
- ActivityManager.TaskDescription
- ActivityOptions
- AlarmManager
- AlarmManager.AlarmClockInfo
- AlertDialog
- AlertDialog.Builder
- AliasActivity
- AppComponentFactory
- Application
- ApplicationErrorReport
- ApplicationErrorReport.AnrInfo
- ApplicationErrorReport.BatteryInfo
- ApplicationErrorReport.CrashInfo
- ApplicationErrorReport.RunningServiceInfo
- AppOpsManager
- AutomaticZenRule
- DatePickerDialog
- Dialog
- DialogFragment
- DownloadManager
- DownloadManager.Query
- DownloadManager.Request
- ExpandableListActivity
- Fragment
- Fragment.SavedState
- FragmentBreadCrumbs
- FragmentContainer
- FragmentController
- FragmentHostCallback
- FragmentManager
- FragmentManager.FragmentLifecycleCallbacks
- FragmentManagerNonConfig
- FragmentTransaction
- Instrumentation
- Instrumentation.ActivityMonitor
- Instrumentation.ActivityResult
- IntentService
- KeyguardManager
- KeyguardManager.KeyguardDismissCallback
- KeyguardManager.KeyguardLock
- LauncherActivity
- LauncherActivity.IconResizer
- LauncherActivity.ListItem
- ListActivity
- ListFragment
- LoaderManager
- LocalActivityManager
- MediaRouteActionProvider
- MediaRouteButton
- NativeActivity
- Notification
- Notification.Action
- Notification.Action.Builder
- Notification.Action.WearableExtender
- Notification.BigPictureStyle
- Notification.BigTextStyle
- Notification.Builder
- Notification.CarExtender
- Notification.CarExtender.Builder
- Notification.CarExtender.UnreadConversation
- Notification.DecoratedCustomViewStyle
- Notification.DecoratedMediaCustomViewStyle
- Notification.InboxStyle
- Notification.MediaStyle
- Notification.MessagingStyle
- Notification.MessagingStyle.Message
- Notification.Style
- Notification.WearableExtender
- NotificationChannel
- NotificationChannelGroup
- NotificationManager
- NotificationManager.Policy
- PendingIntent
- Person
- Person.Builder
- PictureInPictureParams
- PictureInPictureParams.Builder
- Presentation
- ProgressDialog
- RemoteAction
- RemoteInput
- RemoteInput.Builder
- SearchableInfo
- SearchManager
- Service
- SharedElementCallback
- TabActivity
- TaskStackBuilder
- TimePickerDialog
- UiAutomation
- UiModeManager
- VoiceInteractor
- VoiceInteractor.AbortVoiceRequest
- VoiceInteractor.CommandRequest
- VoiceInteractor.CompleteVoiceRequest
- VoiceInteractor.ConfirmationRequest
- VoiceInteractor.PickOptionRequest
- VoiceInteractor.PickOptionRequest.Option
- VoiceInteractor.Prompt
- VoiceInteractor.Request
- WallpaperColors
- WallpaperInfo
- WallpaperManager
Exceptions