Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are differences between JobIntentService and IntentService?

I do not understand what difference between those two APIs. I mean when to use the first one. Why is there JobIntentService ? Thanks in advance

like image 415
Moahemd Abdelkhaleq Avatar asked Nov 11 '18 20:11

Moahemd Abdelkhaleq


People also ask

What is difference between IntentService and JobIntentService?

Both work same but the only difference with JobIntentService is that JobIntentService gets restarted if the application gets killed while the service was executing.

What is the purpose of IntentService?

IntentService is an extension of the Service component class that handles asynchronous requests (expressed as Intent s) on demand. Clients send requests through Context.

What is the difference between service and intent service and how is each used?

Service is a base class of service implementation. Service runs on the application's main thread which may reduce the application performance. Thus, IntentService , which is a direct subclass of Service is available to make things easier. The IntentService is used to perform a certain task in the background.

Does IntentService run on background thread?

The Service runs in background but it runs on the Main Thread of the application. The IntentService runs on a separate worker thread.


3 Answers

I would recommend to read this article explaining about the difference between intent service and job intent service. When we look for the first time at these terms Service, IntentService, JobIntentService they would look almost similar - in one way or other they would perform some operations in background (which user does not notice). But there is few difference in the way they operate,

  • Service - This runs on the same main thread which invokes this service and performs some background operation. For any long running operation happening on the main thread it is recommended to create a new thread and do the job (eg; Handler) by not impacting the main thread's performance.

    Drawback : Runs on main thread

  • IntentService - Intent service also helps in doing some long running (indefinite) background task. The only difference is that it creates a new thread to perform this task and does not run on the main thread. Does the given job on it's onHandleIntent.

    Drawback: The job given to the IntentService would get lost when the application is killed

  • JobIntentService - Job intent service is very similar to IntentService but with few benefits like the application can kill this job at any time and it can start the job from the beginning once the application gets recreated/up.

But from Oreo, if the app is running in background it's not allowed to start the service in background. Android asks us to start the service explicitly by context.startForegroundService instead of context.startService and when the service is started within 5 seconds it must be tied to the notification to have a UI element associated with it.

Reference : https://developer.android.com/about/versions/oreo/background.html

like image 100
Tom Taylor Avatar answered Oct 25 '22 01:10

Tom Taylor


Both work same but the only difference with JobIntentService is that JobIntentService gets restarted if the application gets killed while the service was executing. OnHandleWork() get's restarted after the application get's killed.

like image 30
Dev Avatar answered Oct 24 '22 23:10

Dev


Basically, the two follow the same role, the difference being that an IntentService it is a base class for Service that handles an explicit asynchronous request with Intent on demand, it is starts through the startService (passing the Intent of the service), from hence the service is started as you wish, from the Android Oreo JobIntentService it also performs work processing however it is able to keep running in older versions, it also makes a process simpler. More in fact the 2 APIs have the same follow up. For the Execution of the work from the Oreo uses if JobScheduler.enqueue already in the older versions of the platform, it will be used Context.startService

Hope this helps.

like image 2
MarcosApps Avatar answered Oct 25 '22 01:10

MarcosApps