I developed an Android app in kotlin which should launch a Maplestory game server service on foreground when clicking Start button and stop the service when clicking Stop button.
Application will throw “Service not registered” error after step 3:
- Click the start button to launch the service.
- Minimize the app and back to desktop.
- Re-enter the app and click the stop button.
So I’m confused what’s the right approach to launch and terminate the service ?
Method when clicking buttons:
https://github.com/speedyHKjournalist/MapleServerAndroid/blob/main/app/src/main/java/com/mapleserver/MainCompose.kt#L238
Service definition:
https://github.com/speedyHKjournalist/MapleServerAndroid/blob/main/app/src/main/java/com/mapleserver/ServerService.kt
fun doBindService(
context: Context,
connection: ServiceConnection,
serviceIntent: Intent
) {
context.bindService(serviceIntent, connection, Context.BIND_AUTO_CREATE)
}
fun startMapleServer(context: Context, serviceIntent : Intent) {
startForegroundService(context, serviceIntent)
}
fun stopMapleServer(context: Context, serviceIntent : Intent, connection : ServiceConnection) {
context.unbindService(connection)
context.stopService(serviceIntent)
}
Activity com.mapleserver.MainActivity has leaked ServiceConnection MainComposeKt$MainCompose$mConnection$1@70de9fd that was originally bound here
android.app.ServiceConnectionLeaked: Activity com.mapleserver.MainActivity has leaked ServiceConnection MainComposeKt$MainCompose$mConnection$1@70de9fd that was originally bound here
at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:1953)
at android.app.LoadedApk.getServiceDispatcherCommon(LoadedApk.java:1825)
at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:1804)
at android.app.ContextImpl.bindServiceCommon(ContextImpl.java:1976)
at android.app.ContextImpl.bindService(ContextImpl.java:1907)
at android.content.ContextWrapper.bindService(ContextWrapper.java:822)
at MainComposeKt.doBindService(MainCompose.kt:243)
at MainComposeKt.MainCompose(MainCompose.kt:80)
at com.mapleserver.MainActivity$onCreate$1$1$1$1.invoke(MainActivity.kt:36)
at com.mapleserver.MainActivity$onCreate$1$1$1$1.invoke(MainActivity.kt:35)