3

I have a use-case where, whenever a transaction is completed or failed, I have to wait in background(not going to freeze the UI) for 5 minutes and call a piece of code without user intervention. So AFAIK I need to implement Background Service for this.

I want to know which would be better for my scenario.

  1. Workermanager ( JetPack )
  2. Jobscheduler ( for API 14 - 21, Firebase JobDispatcher)
  3. IntentService

And in Oreo and above, if I run background service will it show in the notification that the App is running in the background?

Aman Verma
  • 3,155
  • 7
  • 30
  • 60
  • Somehow i can't set compileSDKVersion to 25 due to one external SDK i am using. And i need the Job to schedule after 3 minutes of a particular event and unfortunately, there is no setinitialDelay for below 26. I could accept your answer but it doesn't solve my problem. I am using FirebaseJobDispatcher instead. But your answer will surely help other users seeking problem regarding background task execution. So i will accept your answer. – Aman Verma Jun 23 '18 at 13:14

3 Answers3

6

Now the recommended way to do background processing would be Jetpack WorkManager API. I will cite official documentation for the reasons:

WorkManager chooses the appropriate way to run your task based on such factors as the device API level and the app state. If WorkManager executes one of your tasks while the app is running, WorkManager can run your task in a new thread in your app's process. If your app is not running, WorkManager chooses an appropriate way to schedule a background task--depending on the device API level and included dependencies, WorkManager might use JobScheduler, Firebase JobDispatcher, or AlarmManager. You don't need to write device logic to figure out what capabilities the device has and choose an appropriate API; instead, you can just hand your task off to WorkManager and let it choose the best option.

In addition, WorkManager provides several advanced features. For example, you can set up a chain of tasks; when one task finishes, WorkManager queues up the next task in the chain. You can also check a task's state and its return values by observing its LiveData; this can be useful if you want to show UI indicating your task's status.

So instead of worrying every time which background processing to choose (as every task has it's recommended and appropriate way), you can simply use WorkManager and it will do it's job.

This is considering the following gotcha:

WorkManager is intended for tasks that require a guarantee that the system will run them even if the app exits, like uploading app data to a server. It is not intended for in-process background work that can safely be terminated if the app process goes away; for situations like that, we recommend using ThreadPools.

P.S. As WorkManager API is using JobScheduler, Firebase JobDistpacher or AlarmManager under the hood, you must consider minimum API levels for used functionality. JobScheduler requires minimum API 21, Firebase JobDispatcher requires minimum API 14 and Google Play Services.

For the full documentation check: https://developer.android.com/topic/libraries/architecture/workmanager

For your second question: as far as I know you will always see that notification, as it is notifying user that your app is consuming battery. The notification may be disabled by the user from settings in Android Oreo 8.1.

Community
  • 1
  • 1
Arman P.
  • 4,314
  • 2
  • 29
  • 47
  • There is one problem with it, initialdelay() which is 5 minutes is not there for devices lower than API 26. And regarding the second answer, I have never seen apps like FB showing notification. Aren't they consuming battery or can it be disabled programmatically? – Aman Verma Jun 05 '18 at 21:29
  • @AmanVerma Where did you get that `initialdelay` is API > 25? For the notification I think if you define the task that will be scheduled by `JobScheduler` or `JobDispatcher`, it won't show a notification, haven't tested though. – Arman P. Jun 05 '18 at 21:34
  • Yes for API 26 or above, I have the option of setting the initial delay to the OnetimeWorkRequest. Thanks for the notification thing, I will test it and update the findings. – Aman Verma Jun 05 '18 at 21:43
  • @AmanVerma You just need to target latest API, the `initialdelay` will work with older APIs (min api). – Arman P. Jun 05 '18 at 21:45
  • are you sure about that? because wheneve i setinitialdelay i get a warning that Required Api 26 current min is 16 – Aman Verma Jun 06 '18 at 11:52
  • @AmanVerma I've updated answer to reflect minimum APIs needed. Note also that if you want maximum compatibility you must also add `Firebase JobDispatcher` to your `build.gradle`. Your minimum SDK of 16 is ok. What's your target SDK? You must use target SDK of 26 or 27, best practice is to always use latests available SDK level as target SDK. – Arman P. Jun 06 '18 at 13:12
  • I love to use `WorkManager` because of the elegance and simple implementation, but considering your answer, I think `WorkManager` will just become a waste in no time after the `minimalSdk` is 21. And also if I want to do the hard work by myself for implementing constraints manually, using `AlarmManager` and/or `BroadcastReceiver` would be sufficient and can trim down the apk size of my application. – HendraWD Sep 18 '18 at 08:47
1

Going forward, the official android documentation suggests that you use a JobScheduler in place of a background service.

In many cases, apps that previously registered for an implicit broadcast can get similar functionality by using a JobScheduler job. For example, a social photo app might need to perform cleanup on its data from time to time, and prefer to do this when the device is connected to a charger. Previously, the app registered a receiver for ACTION_POWER_CONNECTED in its manifest; when the app received that broadcast, it would check whether cleanup was necessary. To migrate to Android 8.0 or higher, the app removes that receiver from its manifest. Instead, the app schedules a cleanup job that runs when the device is idle and charging. https://developer.android.com/about/versions/oreo/background#services

TomH
  • 2,581
  • 1
  • 15
  • 30
1

WorkManager is probably (eventually) the solution you are looking for. It acts as an abstraction, deciding whether to use JobScheduler (if it's available) Firebase JobDispatcher (if it's available) or falling back to a default implementation otherwise. This way, you get the best of all worlds. It's still in alpha, however, so you may want to at least consider other options.

If you choose not to use WorkManager, a combination of JobScheduler and JobDispatcher is probably appropriate (see here).

However, if you target devices without Google Play Services below API 22, you will need to use another solution. In that case AlarmManager may be what you are looking for, since you need a delayed task with guaranteed execution. Using an IntentService for this is possible, but not as easy. It involves introducing a delay mechanism of some kind, of which there are several choices.

Note that since you are using a batching mechanism if you use one of the Job APIs or WorkManager, you will not see a notification in Oreo. AlarmManager/IntentService based solutions may show a notification, but likely not for very long, since the tasks are fairly short. This is especially true for AlarmManager.

emerssso
  • 2,376
  • 18
  • 24
  • There is one problem with it, initialdelay() which is 5 minutes is not there for devices lower than API 26. So should i use Job scheduler? – Aman Verma Jun 05 '18 at 21:32
  • 1
    @emerssso WorkerManager API will work without Google Play Services, as it will use AlarmManager under the hood in that case. – Arman P. Jun 05 '18 at 21:43
  • Correct, I was speaking as an alternative to JobDispatcher (if you didn't want to use WorkManager since it's in alpha). Will try and rephrase. – emerssso Jun 05 '18 at 21:47
  • @AmanVerma I'm not familiar with the initialdelay() you're referencing. I'd use AlarmManager to schedule an alarm 5min out with `setExact()`. – emerssso Jun 05 '18 at 21:49
  • So @emerssso what are the complications I would face if I use Job scheduler? Is there any limitation prior to Oreo? – Aman Verma Jun 05 '18 at 22:03
  • 1
    Job scheduler is only available in API 21+, it's not around at all if you need something lower. – emerssso Jun 13 '18 at 16:10