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

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0
Solution

Alessandro71

Well-Known Member
Licensed User
Longtime User
Yes, onTimeout is delegated to B4A Service_Timeout event.

StopForeground should be enough: https://developer.android.com/refer...ServiceInfo#FOREGROUND_SERVICE_TYPE_DATA_SYNC
Though it is worth testing with StopService as well.

my actual Sub is as follows

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

but still, after 6 hours I get an untrapped exception

B4X:
android.app.RemoteServiceException$ForegroundServiceDidNotStopInTimeException: A foreground service of type dataSync did not stop within its timeout: ComponentInfo{com.xxx.test/com.xxx.test.logo}
    at android.app.ActivityThread.generateForegroundServiceDidNotStopInTimeException(ActivityThread.java:2504)
    at android.app.ActivityThread.throwRemoteServiceException(ActivityThread.java:2466)
    at android.app.ActivityThread.-$$Nest$mthrowRemoteServiceException(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2820)
    at android.os.Handler.dispatchMessage(Handler.java:107)
    at android.os.Looper.loopOnce(Looper.java:257)
    at android.os.Looper.loop(Looper.java:342)
    at android.app.ActivityThread.main(ActivityThread.java:9638)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:619)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:929)

to be clear: it's fine for me that the operating system will stop the service, I'm just searching a way to avoid the untrapped exception, that show up in the "ANR & Crash" section of the Play Console.
 
Upvote 0

Magma

Expert
Licensed User
Longtime User
Hi @Alessandro71

you can read here - I have the same problem - with Android 15+ devices

As I can understand is the limit of 6hours... running service... So we must not go at that limit... we must stop the service "before the limit" and as I understand will automatically restart! I ve just upload at the production a version of my app that in ~4 hours stopping the service.

I will inform for the results.
 
Upvote 0

Magma

Expert
Licensed User
Longtime User
Hi there again... i am here to inform..

This never worked for me - in android 15+... right... 99.99% - with shortservice datasync...
Private Sub Service_Timeout(Params As Map)
log("if ever is here... to see")
Service.StopForeground(xx)
'StopService(Me) 'even swap with the above... the same..
End Sub

So after tests... and uploading in production - i am in the same point...

What i ve tried to do...

Ofcourse having service_timeout as Erel suggests, plus:
At the same service i have a timer, that every >4 hours stop service if android sdk >34 (checking version and then stopservice) - ok that is working "sometimes" and the happy part of it... is that not need to startservice... automatically runs again...

but as I can understand this working... if device not sleeping... if sleeps (probably sleeps)... never stop... and getting the same msg...


Exception android.app.RemoteServiceException$ForegroundServiceDidNotStopInTimeException:
at android.app.ActivityThread.generateForegroundServiceDidNotStopInTimeException (ActivityThread.java:2539)
at android.app.ActivityThread.throwRemoteServiceException (ActivityThread.java:2501)
at android.app.ActivityThread.-$$Nest$mthrowRemoteServiceException (Unknown Source)
at android.app.ActivityThread$H.handleMessage (ActivityThread.java:2820)
at android.os.Handler.dispatchMessage (Handler.java:107)
at android.os.Looper.loopOnce (Looper.java:249)
at android.os.Looper.loop (Looper.java:337)
at android.app.ActivityThread.main (ActivityThread.java:9515)
at java.lang.reflect.Method.invoke
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:636)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1005)

So I am at the same point... any idea?
ps: I am having errors only from that... and this will make me problems at Google play soon.. ANR is perfect... but this going to errors and soon... and adds 4%+ - and i am sure that in short future will go more because more Android 15+ devices will be enabled...
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
At the same service i have a timer, that every >4 hours stop service if android sdk >34
and then the service is startet again later? 4H + another 4H is 8H and lead to the timeout.

Your service should NOT EXCEED 6 hours of running in a 24hour timespan.
You should reconsider your complete work with this service.

Let the service ONLY RUN as short as possible. Not for hours. DO NOT exceed 6H in the last 24H. Does not matter how often you stop or start it.
if you exceed 6H in 24H then you run into timeout.

I don´t think i can be of more help in this case. You are completely ignoring suggestions. You are not being inside the google rules at all. Since weeks you get this timeouts.
This is my last post in this regard.
 
Upvote 0

Magma

Expert
Licensed User
Longtime User
Hey, sorry — I think you might’ve missed part of what I said. The service is starting again automatically (and “automatically” is meant ironically — that’s on Google, not you 😅). I’m already using StopService or StopForeground, so there’s really no reason for it to restart. Did you get a chance to check that?

and then the service is startet again later? 4H + another 4H is 8H and lead to the timeout.

if you exceed 6H in 24H then you run into timeout ? this is a rule from Google... and a TIP from you... (I am keeping it... but also i am wondering too)
 
Upvote 0

Magma

Expert
Licensed User
Longtime User
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
if you exceed 6H in 24H then you run into timeout ?
Yes. You can read that in the google documentation.

The system permits an app's dataSync services to run for a total of 6 hours in a 24-hour period, after which the system calls the running service's Service.onTimeout(int, int) method (introduced in Android 15).

I already linked that in the past but here again

Did you get a chance to check that?
No! Why should i? I do not use such long running services in my app so i never run into these timeout.

I only use short running services which do their work in less than 5 minutes. I never run into such timeout.
 
Upvote 0

Magma

Expert
Licensed User
Longtime User
Upvote 0

Alessandro71

Well-Known Member
Licensed User
Longtime User
You are completely ignoring suggestions.

I think the issue here is not about understanding the google documentation, rather than being able to correctly trap the OS exception and stop the service without having a ANR in the android vitals in play console.
apparently (I experienced it myself in one project of mine) the Service_Timeout sub is not called when the system wants to stop the service for having reached its time limit, with the onTimeout call.
is there a way to use all available allowed service time, without incurring in a ANR, by implementing the onTimeout call?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
, by implementing the onTimeout call?
by implementing the onTimeout call?
Erel answered on 3rd of Oct that OnTimeout is delegated to the service
So it is implemented already

Based on google documentation the ANR is already happening when OnTimeout is being called by the system.

For further questions you should contact google.
 
Upvote 0

Magma

Expert
Licensed User
Longtime User
Upvote 0
Top