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, BasicIntentService.java

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

    public BasicIntentService() {
        super("BasicIntentService");
    }

    @Override
    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.setAction(MainActivity.ResponseReceiver.ACTION_RESP);
        broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
        broadcastIntent.putExtra(PARAM_OUTPUT, resultTxt);
        sendBroadcast(broadcastIntent);
    }

    @Override
    public void onDestroy() {
        super.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.


    @Override
    protected void onResume() {
        super.onResume();
        IntentFilter filter = new IntentFilter(ResponseReceiver.ACTION_RESP);
        filter.addCategory(Intent.CATEGORY_DEFAULT);
        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.");
        startService(msgIntent);
    }

    public class ResponseReceiver extends BroadcastReceiver {
        public static final String ACTION_RESP =
                "com.example.service.action.MESSAGE_PROCESSED";

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

Complete example in Github

Search within Codexpedia

Custom Search

Search the entire web

Custom Search