Android Intent Service

  • Unlike regular service and bound servie, Intent Service is doesn’t run on the UI thread.
  • To create an Intent Service class, the class needs to extend from the IntentService.
  • IntentService itself is extended from Service.
  • Intent Service will stop itself when the tasks are completed.
  • Intent Service handle all start asynchronous requests (expressed as Intents) on demand, one at a time before it destroys itself.
  • The communication between intent service can be done through interface, BroadcastReceiver, or event bus libraries.
  • etc.

Here is example of implementing Intent Service in Android.

1. Create a class that extends IntentService,

public class BasicIntentService extends IntentService {
    public static final String PARAM_INPUT = "INPUT_MSG";
    public static final String PARAM_OUTPUT = "OUTPUT_MSG";

    public BasicIntentService() {

    protected void onHandleIntent(Intent intent) {
        String msg = intent.getStringExtra(PARAM_INPUT);
        msg = DateFormat.format("MM/dd/yy h:mm:ss aa", System.currentTimeMillis()) + ": " + msg;
        SystemClock.sleep(5000); // 5 seconds
        String resultTxt = msg + "\n" + DateFormat.format("MM/dd/yy h:mm:ss aa", System.currentTimeMillis()) + ": " + "Ok, done!\n";

        Intent broadcastIntent = new Intent();
        broadcastIntent.putExtra(PARAM_OUTPUT, resultTxt);

    public void onDestroy() {
        Log.d("IntentService", "onDestroy");

2. Register it in the manifest.xml file in the application tag.

<service android:name=".BasicIntentService"/>

3. Add the following in an Activity class.

    protected void onResume() {
        IntentFilter filter = new IntentFilter(ResponseReceiver.ACTION_RESP);
        receiver = new ResponseReceiver();
        registerReceiver(receiver, filter);

    private ResponseReceiver receiver;
    public void intentService(View view) {
        Log.d("intentService", "calling BasicIntentService");
        Intent msgIntent = new Intent(this, BasicIntentService.class);
        msgIntent.putExtra(BasicIntentService.PARAM_INPUT, "Hello, please go to take a nap.");

    public class ResponseReceiver extends BroadcastReceiver {
        public static final String ACTION_RESP =

        public void onReceive(Context context, Intent intent) {
            TextView result = (TextView) findViewById(;
            String text = intent.getStringExtra(BasicIntentService.PARAM_OUTPUT);
            Log.d("onReceive", text);
            result.setText(result.getText().toString() + "\n" + text);

Search within Codexpedia

Custom Search

Search the entire web

Custom Search