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 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.
[Expand]
Inherited Constants | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
![]() | |||||||||||
![]() |
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Creates an IntentService.
|
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Return the communication channel to the service.
| |||||||||||
Called by the system when the service is first created.
| |||||||||||
Called by the system to notify a Service that it is no longer used and is being removed.
| |||||||||||
This method is deprecated.
Implement
onStartCommand(Intent, int, int) instead.
| |||||||||||
Called by the system every time a client explicitly starts the service by calling
startService(Intent) , providing the arguments it supplied and a
unique integer token representing the start request. | |||||||||||
Sets intent redelivery preferences.
|
Protected Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
This method is invoked on the worker thread with a request to process.
|
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
![]() | |||||||||||
![]() | |||||||||||
![]() | |||||||||||
![]() | |||||||||||
![]() |
Creates an IntentService. Invoked by your subclass's constructor.
name | Used to name the worker thread, important only for debugging. |
---|
Return the communication channel to the service. May return null if
clients can not bind to the service. The returned
IBinder
is usually for a complex interface
that has been described using
aidl.
Note that unlike other application components, calls on to the IBinder interface returned here may not happen on the main thread of the process. More information about this can be found in Application Fundamentals: Processes and Threads.
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. |
---|
Called by the system when the service is first created. Do not call this method directly.
Called by the system to notify a Service that it is no longer used and is being removed. The service should clean up an 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.
Called by the system every time a client explicitly starts the service by calling
startService(Intent)
, providing the arguments it supplied and a
unique integer token representing the start request. Do not call this method directly.
For backwards compatibility, the default implementation calls
onStart(Intent, int)
and returns either START_STICKY
or START_STICKY_COMPATIBILITY
.
If you need your application to run on platform versions prior to API
level 5, you can use the following model to handle the older onStart(Intent, int)
callback in that case. The handleCommand
method is implemented by
you as appropriate:
// This is the old onStart method that will be called on the pre-2.0 // platform. On 2.0 or later we override onStartCommand() so this // method will not be called. @Override public void onStart(Intent intent, int startId) { handleCommand(intent); } @Override public int onStartCommand(Intent intent, int flags, int startId) { handleCommand(intent); // We want this service to continue running until it is explicitly // stopped, so return sticky. return START_STICKY; }
Note that the system calls this on your
service's main thread. A service's main thread is the same
thread where UI operations take place for Activities running in the
same process. You should always avoid stalling the main
thread's event loop. When doing long-running operations,
network calls, or heavy disk I/O, you should kick off a new
thread, or use AsyncTask
.
intent | The Intent supplied to 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 START_STICKY_COMPATIBILITY . |
---|---|
flags | Additional data about this start request. Currently either
0, START_FLAG_REDELIVERY , or START_FLAG_RETRY . |
startId | A unique integer representing this specific request to
start. Use with stopSelfResult(int) . |
START_CONTINUATION_MASK
bits.Sets intent redelivery preferences. Usually called from the constructor with your preferred semantics.
If enabled is true,
onStartCommand(Intent, int, int)
will return
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
START_NOT_STICKY
, and if the process dies, the Intent
dies along with it.
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.
intent | The value passed to startService(Intent) .
|
---|