SyntaxHighlighter

Friday, January 18, 2013

How to use IntentService in Android

Hi Guys Today I’m going to discussed about IntentService.The Intentservice is a Service which can start or trigger by Intent.This Service run on it’s own thread so it will not effects on main UI thread.So we can use this service to do time consuming data processing.
When this Service receive intent which include some data for doing job.This job put in queue and execute them in sequence and asynchronously.After the job done,send data to where they necessary and stop itself.


Then we can receive data using Broadcast Receivers.
So Today I have created example for you.you can download it from GitHub.In this example we create text processing example with some delay and after that processing we send data back to our activity and catch result using Broadcast Receiver.
In our IntentService (ExampleIntentService.java)

we receive some text here from intent,then we convert it to uppercase,wait few second,then we create new Intent with processed data and action.then we send Broadcast with that intent.Hey this is dummy processing example in you case this may be uploading/downloading some data from internet ;-).And also add this service to manifest file.


Inside our activity I create BroadCastReceiver to catch processed data

It takes processed data and show in TextView.For catch this data we have to register this Receiver.To do that we add following code to activity.But Make sure unregister this receiver in onPause method.

Finally we can call our Intent service like following

Hope you learn something ;-)