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?
 

Alessandro71

Well-Known Member
Licensed User
Longtime User
Erel answered on 3rd of Oct that OnTimeout is delegated to the service
So it is implemented already

yes, I've read that.
experimental evidence suggests that onTimeout is called after the OS has already killed the offending service, thus not giving the app the chance to perform a clean close-out.
as for the actual need (running a service for more that 8 hours) without questioning the rationale behind it, I think we should use other methods, like mediaPlayback, and streaming a mute sound: maybe this kind of service is allowed to run with no limits.
 
Upvote 0

Magma

Expert
Licensed User
Longtime User
I am trying to figure first in my brain - how to make that work...

So I ve split the time at parts.... for my job every 6 minutes starting again the service (and may be the jobs will do... will be about 1 minute)...

is that going to work (ofcourse must be sure that my job will end in one minute... or can maximize it to 4minutes to be sure)...

What do you think ??? - didn't try it...

B4X:
' Service Module: TimerService

Sub Process_Globals
    Private timer1 As Timer
    Private RunDurationMillis As Long = 60 * 1000      ' Run for 1 minute
    Private RestDurationMillis As Long = 6 * 60 * 1000 ' Wait/restart after 6 minutes
    Private fgNotifID As Int = 51042 'datasync
End Sub

Sub Service_Create
    timer1.Initialize("timer1", RunDurationMillis) ' 1 minute active period
End Sub

Sub Service_Start(StartingIntent As Intent)
    StartForeground
    timer1.Enabled = True
End Sub

Sub StartForeground
    Dim n As Notification
    n.Initialize
    n.Icon = "icon"
    n.SetInfo("1-Minute Worker", "Working for 1 minute...", Main)
    Service.StartForeground(fgNotifID, n)
End Sub

Sub timer1_Tick
    DoWorkLoop
    timer1.Enabled = False
    Service.StopForeground(fgNotifID)
    StopService("")
    ScheduleNextRun
End Sub

Sub DoWorkLoop
    ' Put your actual work here - usually a for loop or work tasks
    For i = 1 To 100
        Log($"Do work step ${i}"$)
        Sleep(100) ' Simulate processing per step
    Next
End Sub

Sub ScheduleNextRun
    Dim nextRun As Long = DateTime.Now + RestDurationMillis
    StartServiceAtExact("TimerService", nextRun, True)
    Log($"Scheduled service to run again at: ${DateTime.Time(nextRun)}"$)
End Sub

Sub Service_Destroy
    timer1.Enabled = False
    Service.StopForeground(fgNotifID)
End Sub
 
Upvote 0
Top