My app will call startForegroundService(intent)
in the onCreate
of the MainActivity
. And I put startForeground(ON_SERVICE_CONNECTION_NID, notification)
in both the onCreate
and the startCommand
of the Service
.
But I'm still receiving this error occasionally.
Exception android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground()
android.app.ActivityThread$H.handleMessage (ActivityThread.java:1775)
android.os.Handler.dispatchMessage (Handler.java:105)
android.os.Looper.loop (Looper.java:164)
android.app.ActivityThread.main (ActivityThread.java:6541)
java.lang.reflect.Method.invoke (Method.java)
com.android.internal.os.Zygote$MethodAndArgsCaller.run (Zygote.java:240)
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:767)
How could this happen?
Is it possible that the onCreate
or startCommand
doesn't get called in 5 seconds?
If so, how should we call startForegroundService
with no error?
Updated 2017-12-09
I don't know why every one said that it's the same as this question: Context.startForegroundService() did not then call Service.startForeground() You can even find a comment from me over there.
The reason why it's different is what you can see at the first line of this question. I have put the startForegroundService
and startForeground
at the right place, but it's still showing this error randomly.
And Here's the Google issue tracker: https://issuetracker.google.com/issues/67920140
Stopping the service before this happens will cause a crash.
This is another problem about the lifecycle of the service. The assertion may be incorrectly triggered after the service closed.