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.
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
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
Comments