I am using Service
Class on the Android O OS.
I plan to use the Service
in the background.
The Android recommendation states that startService should use startForegroundService.
If you use startForegroundService, the Service
throws a Context.startForegroundService()
did not then call Service.startForeground()
error.
What's wrong with this?
Why this issue is happening is because Android framework can't guarantee your service get started within 5 second but on the other hand framework does have strict limit on foreground notification must be fired within 5 seconds, without checking if framework had tried to start the service.
This is definitely a framework issue, but not all developers facing this issue are doing their best:
startForeground a notification must be in both onCreate and onStartCommand, because if your service is already created and somehow your activity is trying to start it again, onCreate won't be called.
notification ID must not be 0 otherwise same crash will happen even it's not same reason.
stopSelf must not be called before startForeground.
With all above 3 this issue can be reduced a bit but still not a fix, the real fix or let's say workaround is to downgrade your target sdk version to 25.
And note that most likely Android P will still carry this issue because Google refuses to even understand what is going on and does not believe this is their fault, read #36 in here: https://issuetracker.google.com/issues/76112072.
Your app will crash if you call
Context.startForegroundService(...)
and then callContext.stopService(...)
beforeService.startForeground(...)
is called.I have a clear repro at: https://github.com/paulpv/ForegroundServiceAPI26/blob/repro/app/src/main/java/com/github/paulpv/foregroundserviceapi26/MainService.kt#L28
I have opened a bug on this: https://issuetracker.google.com/issues/76112072
Several bugs on this have been opened and closed Won't Fix.
Hopefully mine with clear repro steps will make the cut.
I am facing same issue and after spending time found a solutons you can try below code. If your using
Service
then put this code in onCreate else your usingIntent Service
then put this code in onHandleIntent.Please don't call any StartForgroundServices inside onCreate() method, you have to call StartForground services in onStart() command after make the worker thread otherwise you will get ANR always , so please don't write complex login in main thread of onStatcommand();
public class Services extends Service {
}
I know it's a late answer but, I think it could help in future, I just used
JobIntentService
instead ofIntentService
that include its JobScheduler and handle everything for me. look at this exampleI've been researching this issue and this is what I've discovered so far. This crash could happen if we have code similar to this:
MyForegroundService.java
MainActivity.java
The exception is thrown in the following block of the code:
ActiveServices.java
This method is executed before
onCreate()
ofMyForegroundService
because Android schedules the creation of the service on the main thread handler butbringDownServiceLocked
is called on aBinderThread
, wich is a race condition. It means thatMyForegroundService
didn't have a chance to callstartForeground
which will cause the crash.To fix this we have to make sure that
bringDownServiceLocked
is not called beforeonCreate()
ofMyForegroundService
.By using sticky broadcasts we make sure that the broadcast doesn't get lost and
stopReceiver
receives the stop intent as soon as it has been registered inonCreate()
ofMyForegroundService
. By this time we have already calledstartForeground(...)
. We also have to remove that sticky broadcast to prevent stopReceiver being notified next time.Please note that the method
sendStickyBroadcast
is deprecated and I use it only as a temporary workaround to fix this issue.