Services vs IntentServices in Android

Services vs IntentServices in Android

Hello everyone, in this article we ae going to talk about android services and intent services. We are going to make explanations and then we will make a comparisation between them.

Let's get started.

Firstly what are services and Intent Services in android. We should take a look at them firstly. Service is a structure of the application which performs the operations background that the user do not have to see what is happening. Generally long-time background operations are performing via services. Service class is a service class implementation class. IntentService is a base class for Services that handle asynchronous requests (expressed as Intents) on demand during the normal program operations. Application start the IntentService via startService(intent). Intent service make operations at a seperate process not like services and terminate itself when it has done.

  • Service use the main thread of the application but IntentService start a new seperate thread and make operations on this seperate thread. According to differances the service can reduce the appication performance compared to IntentService, also creating a new thread will increase the energy consumption acompared to Services because starting a new process need something else.
  • Intent services works with queues. W can start it from an activity with an intent, this intent will be taken by onHandleIntent(). When this intent completed, this related intent service will be kill itself. But services need a stopself() to kill.
  • IntentService implements onBind() that returns null. This means that the IntentService can not be bound by default. This means you do not have to implement the onBind override method for IntentServices but Services you have to implement onBind method and make service bound to related class. You can use the onBind method with the Local Binder to be bound to another class or activity.
  • IntentService implements onStartCommand() which sends Intent to queue and to onHandleIntent().

Below you will see the example Service and IntentService implementations with override methods.

TheService.java


public class TheService extends Service {

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);
    }

    public class MyLocalBinder extends Binder {
        TheService getService() {
            return TheService.this;
        }
    }
}

TheIntentService.java


public class TheIntentService extends IntentService {

    public TheIntentService(String name) {
        super(name);
    }

    @Override
    protected void onHandleIntent(@Nullable Intent intent) {

    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        //onBind is not required for the intent service, it is defaultly returns null
        return super.onBind(intent);
    }
}

You can use the services for operations which have to be done during the normal program operating and do not need the run when program destroyed. I want to underline the situation which is not the pause the program. Because when you destroyed the program main thread, the service will be destroyed. But the IntentService will not be destroyed at this situation. So you can use very long-term operations with IntentServices. For example you can check the server every night at same time even with IntentService. The application do not have to be running at that time.

IntentServices have some advantages compare to Services:

IntentServices are good solution for thread management of the application. Because when an intent started the thread take it process it and after kill its thread automatically. The developer do not have to check the thread is completed or not to save some free spaces on memory and cpu.

The Intent services also add all intents to a queue and process them one by one. And when the queue finished, the Intent Service kills itself automatically. So the developer do not need to check the service intents and handle the destroying the services.

Intent Service is a good implementation way of a Service which accomodate most of your application requirements.

You have to be carefull about deprecation after Android API 30

This class was deprecated in API level R.
IntentService is subject to all the background execution limits imposed with Android 8.0 (API level 26). Consider using WorkManager or JobIntentService, which uses jobs instead of services when running on Android 8.0 or higher.
"

That is all in this article.

Have a good Intent Servicing.

I wish you all healthy days.

Burak Hamdi TUFAN


Tags


Share this Post

Send with Whatsapp

Post a Comment

Success! Your comment sent to post. It will be showed after confirmation.
Error! There was an error sending your comment. Check your inputs!

Comments

  • There is no comment. Be the owner of first comment...