Service and BOOT_COMPLETED on Android O

Aug 10, 2017 · 1 min read

Starting from Android O, you can not start a service from a background app without being blessed with an exception:

java.lang.IllegalStateException: Not allowed to start service Intent (my_service) : app is in background

If you still need to launch a service at device start up, you can now use the new JobIntentService.

Add your BroadcastReceiver and JobIntentService to your manifest:

Enqueue work for your JobIntentService:

Define your JobIntentService:

And that’s it. This will either directly start the service (when running on pre-O platforms) or enqueue work for it as a job (when running on O and later). No matter what the platform is, everything you pass in enqueueWork will ultimately appears in onHandleWork.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade