2

I use a com.firebase.jobdispatcher.JobService to sync data, I start it by scheduling a Job using FirebaseJobDispatcher.

Problem: I don't like the delay before it runs on my very first app start.

Is there a way in which I can start the service, on Android O, as a foreground service (with a notification) the first time I use it? (instead of scheduling it as a job)

I tried:

ContextCompat.startForegroundService(context, intent);

but this does not call the onCreate of the JobService.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Frank
  • 12,010
  • 8
  • 61
  • 78

2 Answers2

3

Conclusion: use two services.

A com.firebase.jobdispatcher.JobService cannot be started in the foreground.

A normal Service cannot be started as a scheduled Job.

I created two service classes that both start the same sync-flow: fixed.

Frank
  • 12,010
  • 8
  • 61
  • 78
2

I would advise you to use the new JobIntentService, introduced in Oreo, instead of FirebaseJobDispatcher. Then you can start a background service from the foreground of your app (background services created in the background is not freely allowed now, you only get 5 seconds to do stuff before you have to show a notification). With JobIntentService you don't have those limits.

You also don't have to show a notification for the syncing if you don't want to. But if you really need to do it, do it in the onHandleWork() method in JobIntentService.

This article explains the new background execution limits in Oreo quite well:

https://medium.com/til-kotlin/jobintentservice-for-background-processing-on-android-o-39535460e060

JobIntentService is really easy to use and solves any problems you might face with the new background limits. It's also very easy to convert a normal IntentService like used in pre-Oreo to a JobIntentService. Because it's in the support library, it is backported to previous versions. In Oreo and up it lets the OS decide when to schedule jobs (you don't have to work directly with a JobDispatcher), and pre-Oreo a normal IntentService will be started. The service starts and handles work by calling the static helper enqueueWork() method.

See here for more details:

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

  • Oh forgot to mention: JobIntentService automatically handles wake locks for you. – LaurieScheepers Oct 18 '17 at 09:53
  • Thanx for the elaborate answer. I read those articles, but where exactly do you see that on Android O a JobIntentService can be started as a normal service, not scheduled and started X seconds later? That is what my question is about. I do not want to schedule the task. – Frank Oct 18 '17 at 10:01
  • Ah, ok then I am not sure. For our intents and purposes it is fine that the service is started when the OS decides to start it. With your use case, I think you will have to show the "syncing" notification, as it can be a long process. – LaurieScheepers Oct 18 '17 at 12:28
  • Yes, that's what I would like, but not in all situations. I'd like one service that is both schedulable and foreground-able. Seems not possible. – Frank Oct 18 '17 at 12:37