I am currently working on an application for playing sound, and I am using a running service to play music in the background. I start and get attached to the service using the following code.
val intent = Intent(context, MusicService::class.java)
context.startService(intent)
context.bindService(intent, serviceConnection, 0)
He moves to the forefront of the game and gets a downgrade when the music pauses.
service.startForeground(NOTIFY_ID, notification)
service.stopForeground(false)
The service is still working. But when the notification is paused (deleted) by the user in a paused state, the service is reset after a few seconds, giving this error.
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myplayer, PID: 4380
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(Native Method)
com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
This only happens in Oreo, and I already read about background restrictions in Oreo. But the following points are deceiving me.
- This service is a related service (not subject to restrictions)
- Context.startForegroundService() ( ).
- , , .
? ? , - , .