Android Question ForegroundServiceDidNotStopInTimeException for service type dataSync

Alessandro71

Well-Known Member
Licensed User
Longtime User
occasionally, I get a crash report with the following text

B4X:
android.app.RemoteServiceException$ForegroundServiceDidNotStopInTimeException: A foreground service of type dataSync did not stop within its timeout:

I actually have a foreground service of type dataSync where I've put the following code

B4X:
Private Sub Service_Timeout(Params As Map)
    Service.StopForeground(99)
End Sub

where 99 is the id used for StartForegound, but still getting the crash.
This exception is not even caught by the Application_Error sub in the Starter service.
Is there a way to trap it?
 

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

Alessandro71

Well-Known Member
Licensed User
Longtime User
it can be the case, since I've found the message after leaving the phone running a full night.
i think the Service.onTimeout(int, int) method is not our Service_Timeout event
@Erel how can we address this?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
i think the Service.onTimeout(int, int) method is not our Service_Timeout event
Read the page i´ve linked. There is a use case written when the system is calling Service.OnTimeout
There is a limit of 6 hours within 24hours for Datasyncservices.
 
Upvote 0

Alessandro71

Well-Known Member
Licensed User
Longtime User
I don't think Service_Timeout is called by Service.onTimeout.
In my Service_Timeout I have StopForeground, but I still get the exception.
Or maybe I should also call StopService(Me)?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
but I still get the exception
If the service is running >6h in foreground (in the last 24h) than this is expected based on the info in googles documentation.

When a foreground service reaches the six-hour limit, the service has a few seconds to call Service.stopSelf(). When the system calls Service.onTimeout(), the service is no longer considered a foreground service. If the service does not call Service.stopSelf(), the system throws an internal exception. The exception is logged in Logcat with the following message:

Fatal Exception: android.app.RemoteServiceException: "A foreground service of
type [service type] did not stop within its timeout: [component name]"

I don´t know if B4A is internally calling
B4X:
Service.stopSelf()
 
Upvote 0

Alessandro71

Well-Known Member
Licensed User
Longtime User
If the service is running >6h in foreground (in the last 24h) than this is expected based on the info in googles documentation.
yes, I understand it is expected behavior, I just need to know how to correctly call StopService.
it is not clear to me yet, if the expected Service.onTimeout() is translated in Service_Timeout in B4A.
in the meantime I added a StopService(Me) in my Service_Timeout, and will experimentally if it fixes the exception.
 
Upvote 0
Top