我正在制作音频播放应用程序,我正在使用一个启动绑定服务在后台播放音乐 . 我使用以下代码启动并绑定到服务 .
val intent = Intent(context, MusicService::class.java)
context.startService(intent)
context.bindService(intent, serviceConnection, 0)
它在播放时被提升为前景,在音乐暂停时被降级 .
// onAudioPlay
service.startForeground(NOTIFY_ID, notification)
// onAudioPause
service.stopForeground(false)
到目前为止服务工作很好 . 但是当用户在暂停状态下刷新(删除)通知时,服务会在几秒钟后崩溃,从而出现此错误 .
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)
这只发生在奥利奥上,我已经读过奥利奥的背景限制 . 但以下几点让我感到困惑 .
此服务是绑定服务(不受限制影响)
我从不使用Context.startForegroundService()来启动服务 . (并且不想使用它)
删除通知时,服务没有't crash when it gets demoted from foreground, it' .
为什么服务崩溃了?我究竟做错了什么?如果有人告诉我这里发生了什么,我很感激 .