Added in API level 3
Deprecated in API level 30

IntentService

abstract class IntentService : Service
kotlin.Any
   ↳ android.content.Context
   ↳ android.content.ContextWrapper
   ↳ android.app.Service
   ↳ android.app.IntentService

IntentService is an extension of the Service component class that handles asynchronous requests (expressed as Intents) on demand. Clients send requests through android.content.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(android.content.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.

Summary

Inherited constants
Public constructors

Creates an IntentService.

Public methods
open 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.

open Unit

open Unit

open Unit
onStart(intent: Intent?, startId: Int)

open Int
onStartCommand(intent: Intent?, flags: Int, startId: Int)

You should not override this method for your IntentService.

open Unit

Sets intent redelivery preferences.

Protected methods
abstract Unit

This method is invoked on the worker thread with a request to process.

Inherited functions

Public constructors

IntentService

Added in API level 3
IntentService(name: String!)

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

Added in API level 3
open fun onBind(intent: Intent!): IBinder?

Deprecated: Deprecated in Java.

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 android.content.Context#bindService. Note that any extras that were included with the Intent at that point will not be seen here.
Return
IBinder? Return an IBinder through which clients can call on to the service.

onCreate

Added in API level 3
open fun onCreate(): Unit

Deprecated: Deprecated in Java.

onDestroy

Added in API level 3
open fun onDestroy(): Unit

Deprecated: Deprecated in Java.

onStart

Added in API level 3
open fun onStart(
    intent: Intent?,
    startId: Int
): Unit

Deprecated: Deprecated in Java.

Parameters
intent Intent?: This value may be null.

onStartCommand

Added in API level 5
Deprecated in API level 30
open fun onStartCommand(
    intent: Intent?,
    flags: Int,
    startId: Int
): Int

Deprecated: Deprecated in Java.

You should not override this method for your IntentService. Instead, override onHandleIntent, which the system calls when the IntentService receives a start request.

Parameters
intent Intent?: This value may be null.
flags Int: Additional data about this start request. Value is either 0 or a combination of android.app.Service#START_FLAG_REDELIVERY, and android.app.Service#START_FLAG_RETRY
startId Int: A unique integer representing this specific request to start. Use with stopSelfResult(int).
Return
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. Value is android.app.Service#START_STICKY_COMPATIBILITY, android.app.Service#START_STICKY, android.app.Service#START_NOT_STICKY, or android.app.Service#START_REDELIVER_INTENT

setIntentRedelivery

Added in API level 5
Deprecated in API level 30
open fun setIntentRedelivery(enabled: Boolean): Unit

Deprecated: Deprecated in Java.

Sets intent redelivery preferences. Usually called from the constructor with your preferred semantics.

If enabled is true, onStartCommand(android.content.Intent,int,int) will return Service#START_REDELIVER_INTENT, so if this process dies before onHandleIntent(android.content.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(android.content.Intent,int,int) will return Service#START_NOT_STICKY, and if the process dies, the Intent dies along with it.

Protected methods

onHandleIntent

Added in API level 3
protected abstract fun onHandleIntent(intent: Intent?): Unit

Deprecated: Deprecated in Java.

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 #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 android.content.Context#startService(Intent). This may be null if the service is being restarted after its process has gone away; see android.app.Service#onStartCommand for details.