'RemoteServiceException only when starting service from callback
My app is starting a foreground service when the user tap on a button. The service starts if and only if the user grants all the permissions (3), here is the code of the fragment:
private val permReqLauncher =
registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { permissions ->
val granted = permissions.entries.all {
it.value
}
if (granted) {
mService.startAnswering()
} else {
if (shouldProvideRationale()) {
Snackbar.make(...
Here is the listener on the button:
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.fab.setOnClickListener {
if (!MotoAnswer.isServiceRunning) {
if (hasPermissions())
mService.startAnswering()
else
permReqLauncher.launch(PERMISSIONS)
} else {
mService.stopAnswering()
}
updateUI()
}
}
The problem is that, if the service is started from the registerForActivityResult, then I get the exception, otherwise all is fine. This means that if it's the first time the user tries to start the service, he get the exception; starting from the second time all is fine.
Here is the stack:
Fatal Exception: android.app.RemoteServiceException Context.startForegroundService() did not then call Service.startForeground(): ServiceRecord{c7a5885 u0 org.punkeroso.motoanswer/.AnswerService} android.app.ActivityThread$H.handleMessage (ActivityThread.java:2047) android.os.Handler.dispatchMessage (Handler.java:106)
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:967)
Do you have any clue on why this is happening? It seems fine to me but, obviously, this is not.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
