Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IntentService + startForeground vs JobIntentService

Since Android Oreo background execution limits, the docs recommend to refactor IntentServices to JobIntentService.

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

JobIntentService runs immediately as an IntentService below Oreo, but schedules a Job on Oreo+

https://developer.android.com/reference/android/support/v4/app/JobIntentService

In what cases would it make sense to run a normal IntentService as a foreground Service with a persistent notification, and when is a JobIntentService better?

One downside I can see in JobIntentService is that it doesn't start immediately.

like image 750
Florian Walther Avatar asked Nov 11 '18 12:11

Florian Walther


People also ask

What is the 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.

Why is JobIntentService deprecated?

This class is deprecated. This class has been deprecated in favor of the Android Jetpack WorkManagerlibrary, which makes it easy to schedule deferrable, asynchronous tasks that are expected to run even if the app exits or the device restarts.

What is the use of JobIntentService?

You can say JobIntentService is a modern way to run the background service from the background application. JobIntentService works in the same way as a Service however it enqueues the work into the JobScheduler on compatible Android targets( SDK 26 or more).

What are the limitations of the IntentService?

Limitations / Drawbacks The IntentService cannot run tasks in parallel. Hence all the consecutive intents will go into the message queue for the worker thread and will execute sequentially.

Should I use intentservice or service for background services?

If you some limited amount of tasks to be performed in the background, then you can use Service, otherwise, you can use IntentService. You can refer to the Android official documentation for the best practice on Background Services. Team MindOrks!

Is it possible to replace intentservice with jobintentservice?

You can use JobIntentService as a replacement for IntentService that is compatible with newer versions of Android. Deprecating the IntentService class will force developers to write error prone custom threaded foreground Services if they need immediate work done that won't die.

What is the difference between intentservice and service in Android?

If you some limited amount of tasks to be performed in the background, then you can use Service, otherwise, you can use IntentService. You can refer to the Android official documentation for the best practice on Background Services.

How do I start a service from an intent?

To start a Service, use the onStartService () function, but to start an IntentService, use Intent, i.e. start the IntentService by calling Context.startService (Intent). Because Service operates on the Main thread, there is a risk that your Main thread will be stopped if you utilise it.


Video Answer


2 Answers

Foreground service is not affected by Doze, but you still have to use wake locks, if you need your task to be continued when the screen is off.

The JobIntentService (which uses the JobScheduler) manages wake locks for you, but you have less control when the job will be started.

I would use the foreground IntentService (or Service) for high priority tasks (e.g. downloading a database) that should run immediatelly and that should not be paused / killed by system.

I would use the JobIntentService in conjunction with AlarmManager to schedule low priority tasks like refreshing the widget's data periodically.

like image 95
Derek K Avatar answered Oct 20 '22 11:10

Derek K


If you want to make long running operation something like Music Player use Foreground Service with notification. because JobIntentService has time execution limit like JobScheduler. ( 10 minutes)

If you think that user don't need to know about your work you can use JobIntentService without notification.

You don't need to worry about Doze mode if user is actively using your app.

There are some cases for Doze mode, according to https://www.bignerdranch.com/blog/diving-into-doze-mode-for-developers/

Light-Doze starts to kick in shortly after both the screen is off and the device is not charging, waiting only a couple minutes before applying the restrictions just to make sure the user has stopped using their phone

For example, user turned screen off while you are computing something. Your task is finished and you want to run background service. In that case your JobIntentService can be deffered, because device can be in doze mode.

However, if you want immediately perform background operation use ForegrounService with WakeLock, because ForegroundService is not working when the screen is off.

like image 26
Valgaal Avatar answered Oct 20 '22 10:10

Valgaal