3

With the new background service restrictions introduced in Oreo, I'm having a hard time finding a place to clarify whether IntentServices can still be launched from a JobScheduler while the app is backgrounded.

I have a very important feature based around geofencing that needs to run an intentService after a fence is broken. I spent a long time setting it up to queue up jobs when fences are broken and the app is in DOZE for Nougat. When the job is finally run by the OS when the window of network connection is reopened, I spin off each job one after the other in an intentService. Can I no longer do this now that I'm technically starting a service while the app is not in the foreground or is this still permitted since I'm using the JobScheduler?

Le2e410
  • 323
  • 4
  • 18

3 Answers3

6

You should not assume that your app is able to start services from a job. In particular, apps are frequently still in a background state when their jobs execute, and calling startService() while in a background state will throw an exception. Of course, it is always legal to call startForegroundService(); though obviously this has UI implications as well.

JobIntentService is explicitly intended to replace IntentService in a way that is compatible with Android O+ background restrictions. You should look into switching to that instead of using the legacy IntentService support class.

ctate
  • 1,379
  • 10
  • 11
0

read this article [Exploring Background Execution Limits on Android Oreo]

https://medium.com/exploring-android/exploring-background-execution-limits-on-android-oreo-ab384762a66c hope it will answer your question

Hardik Mehta
  • 867
  • 1
  • 12
  • 20
  • 1
    I was actually reading this article before I asked this question. Its the same as pretty much 99% of the resources for this new restriction. It says JobScheduler is an alternative but doesn't go into detail on using it or the specifics for spawning IntentServices off of it. – Le2e410 Sep 18 '17 at 14:14
  • @Le2e410 if I am not wrong then in this article it is clearly mentioned that "To begin with the most important thing to note is that if you attempt to call startService() when your application is not in the foreground then an IllegalStateException will be thrown. ". so the exception will be thrown if you try to start your service in the background regardless of the component which starts this service. – Hardik Mehta Sep 19 '17 at 04:06
  • Yeah, that is what I was thinking but I wasnt sure if since the OS is the one that launches the jobs it is a work around. This makes me wonder what happens when a geofence is broken and the OS sends the pendingIntent to your intentService. It seems like geofences will only work now when your app is in the foreground? Thats a really poor oversight if Im not missing something here. – Le2e410 Sep 19 '17 at 15:12
  • @Le2e410 you can use foreground service with START_STICKY as stated in documentation background execution limits will not be applied to the foreground service – Hardik Mehta Sep 20 '17 at 04:24
0

For API 25 and below use background Service or IntentService with a regular startService() call.

For API 26 and above use startForegroundService().

You can also choose to use androidx.core.app.JobIntentService which is using JobScheduler for API 26+, and background service for API 25 and below.

RonTLV
  • 2,376
  • 2
  • 24
  • 38