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?
I called
ContextCompat.startForegroundService(this, intent)
to start the service thenIn service
onCreate
Problem With Android O API 26
If you stop the service right away (so your service does not actually really runs (wording / comprehension) and you are way under the ANR interval, you still need to call startForeground before stopSelf
https://plus.google.com/116630648530850689477/posts/L2rn4T6SAJ5
Tried this Approach But it Still creates an error:-
I Am Using this until the Error is Resolved
I just check the
PendingIntent
null or nor not before calling thecontext.startForegroundService(service_intent)
function.this works for me
This all goes away if you rewrite your service to be started w/ bindService.
An example of how to do this can be seen at: https://github.com/paulpv/ForegroundServiceAPI26/tree/bound
A diff of my "repro" branch can been seen at: https://github.com/paulpv/ForegroundServiceAPI26/compare/repro...bound?expand=1
This error also occurs on Android 8+ when Service.startForeground(int id, Notification notification) is called while id is set to 0.
From Google's docs on Android 8.0 behavior changes:
Solution: Call
startForeground()
inonCreate()
for theService
which you useContext.startForegroundService()
See also: Background Execution Limits for Android 8.0 (Oreo)