kotlin-toolkit
kotlin-toolkit copied to clipboard
Caused by java.lang.IllegalStateException: Not allowed to start service Intent
Device Brand:samsung Model:Galaxy S9 Orientation: Portrait RAM free: 1.2 GB Disk free: 14.3 GB
Operating System Version:Android 10 Orientation: Portrait Rooted:No
Caused by java.lang.IllegalStateException: Not allowed to start service Intent { act=com.readerappnext.MediaService cmp=com.readerappnext/.epub_plugin.MediaService }: app is in background uid UidRecord{3c87fd2 u0a230 TRNB idle change:uncached procs:1 proclist:26821, seq(0,0,0)}
at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1688)
at android.app.ContextImpl.startService(ContextImpl.java:1633)
at android.content.ContextWrapper.startService(ContextWrapper.java:683)
at com.theonelibrary.Application.onCreate(Application.java:74)
at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1190)
at android.app.ActivityThread.handleMakeApplication(ActivityThread.java:7184)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:7134)
at android.app.ActivityThread.access$1600(ActivityThread.java:274)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2102)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:237)
at android.app.ActivityThread.main(ActivityThread.java:8167)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:496)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100)
running latest develop code. Error comes occasionally not every time.
I believe the reason is that we need to use startForegroundService instead of startService here,
https://github.com/readium/kotlin-toolkit/blob/adeb8f452fbd48b6ab7e1132264bcce9ab64ee95/readium/navigator/src/main/java/org/readium/r2/navigator/media/MediaService.kt#L304
Using this workaround:
fun Context.startForegroundServiceCompat(intent: Intent) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(intent)
} else {
startService(intent)
}
}
@mrifni Would you mind testing it out and coming back with your results? Thanks.
fun Context.startForegroundServiceCompat(intent: Intent) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(intent)
} else {
startService(intent)
}
}
where do you want me to put the above code @mickael-menu ? I am using the testapp at the moment.
@mrifni Try this PR, or branch fix/media-service: https://github.com/readium/kotlin-toolkit/pull/95
this issue appeared again even with the above change.
Fatal Exception: java.lang.RuntimeException
Unable to create application com.testapp.Application: android.app.BackgroundServiceStartNotAllowedException: Not allowed to start service Intent { act=com.testapp.epub_plugin.MediaService cmp=com.testapp/.epub_plugin.MediaService }: app is in background uid UidRecord{6c7f82a u0a483 TRNB idle change:idle|uncached procs:0 seq(0,0,0)}
Maybe try applying the same fix here as well? https://github.com/readium/kotlin-toolkit/blob/540c80fc1c57f04d09db45764f103724177aedf9/test-app/src/main/java/org/readium/r2/testapp/Application.kt#L85
But I'm not 100% sure it's correct.
Alternatively, if you don't use the media2 audiobook navigator, you can just remove the call to startService() as you don't need the MediaService.
Applying the fix in Application.kt crashed the app immediately with below error
Context.startForegroundService() did not then call Service.startForeground()
We won't work longer on this code. Please open new issues if you face bugs with the media3 stuff.