- <service android:name=".UploadS3IntentService"android:exported="false" />
IntentService provides a simple way to offload work on a background thread. It handles everything about receiving requests, putting them in a queue, stopping itself, etc. for you. It is also easy to implement, making it the perfect thing to use when you have time-consuming operations to do that don't belong on the Main (UI) thread.
Basic IntentService Example
The abstract class
IntentService is a base class for services, which run in the background without any user interface. Therefore, in order to update the UI, we have to make use of a receiver, which may be either a
BroadcastReceiver or a
BroadcastReceivershould be used if your service needs to communicate with multiple components that want to listen for communication.
ResultReceiver: should be used if your service needs to communicate with only the parent application (i.e. your application).
IntentService, we have one key method,
onHandleIntent(), in which we will do all actions, for example, preparing notifications, creating alarms, etc.
If you want to use you own
IntentService, you have to extend it as follows:
Calling/starting the activity can be done as follows:
Similar to any activity, you can pass extra information such as bundle data to it as follows:
Now assume that we passed some data to the
YourIntentService class. Based on this data, an action can be performed as follows:
The code above also shows how to handle constraints in the
Creating an IntentService
To create an IntentService, create a class which extends
IntentService, and within it, a method which overrides
Sample Intent Service
Here is an example of an
IntentService that pretends to load images in the background. All you need to do to implement an
IntentService is to provide a constructor that calls the
super(String) constructor, and you need to implement the
In order to start an
IntentService, you need to send an
Intent to it. You can do so from an
Activity, for an example. Of course, you're not limited to that. Here is an example of how you would summon your new
Service from an
IntentService processes the data from its
Intents sequentially, so that you can send multiple
Intents without worrying whether they will collide with each other. Only one
Intent at a time is processed, the rest go in a queue. When all the jobs are complete, the
IntentService will shut itself down automatically.