Intent Service in Android : Power with Simplicity

1 min read

Android Development

Dear Developers

In our earlier articles regarding Service android components we saw usage of Service in android. To recall what we studied , I would say services are component of android that run in background  does the work. But we came to know about a fact that even if service component runs in background but operations done inside service remain on UI Thread unless and until we create a separate thread  and offload time consuming task from UI Thread to worker thread.

Again as a developer if you don’t take responsibility to stop a service , it keeps on running and hence consuming device resources like power, memory etc.

 

Why it is required ? ( Intent Service)

 

Service is a sort of Heavy weight champion and complex programming using service can handle multithreading scenario. But if we don’t have that multithreaded requirement means ( accessing service from different threads), do we really need a heavy weight champion like service for our task?

Android OS has provided us another service variant called Intent Service. Intent service has most of the capabilities similar to that of service and also once the task is finished intent service closes itself automatically. So developer don’t have to bother.

In addition to above whenever any work is given to intent service, it performs that work in onHandleIntent() and automatically does the work in background. So that way if the work is time consuming it doesn’t block UIThread. means your UIThread is free for responding to User touches or for showing those cooler animations.

Have a look at the below code and you will got to know how easy it to to create a Intent Service in android. you just need to extend IntentService class.

 

import android.app.IntentService;
import android.content.Intent;

/**
 * Created by anilkukreti on 26/07/17.
 */

public class MyIntentService extends IntentService {

    /**
     * Creates an IntentService.  Invoked by your subclass's constructor.
     *
     * @param name Used to name the worker thread, important only for debugging.
     */
    public MyIntentService(String name) {
        super(name);
    }

    @Override
    protected void onHandleIntent(Intent intent) {

        // do time consuming task in background.


    }
}

But remember similar to service if you want you Intent service to be executed by the system you need to declare that in androidmanifest.xml. I think its worth repeating

 

But remember similar to service if you want you Intent service to be executed by the system you need to declare that in androidmanifest.xml.

 

Conclusion

Service , Intent service has their pros and cons , so its the application requirement that motivates a developer to use Service or intent service. Intent services are Simple but they are powerful enough to do most of the tasks in android. They are used heavily around the globe. 🙂

 

 

Home work :

Write and intent service that can help in writing a text file to android shared storage location.

Write and intent service that can help in reading a text file from android shared storage location.