Android Question Some Problems for Services found at Play Console (starter.handleStart?)

Magma

Expert
Licensed User
Longtime User
Hi there...

well.. I understand that sometimes, some SDK works different... but many times developers ignore some things...but this is a custom app for a client that works with Google Play so I must be into limits of Errors. I hope get help for those.

As i understand the problem coming from Services (the following errors sendxml, searchbtn, sendit fixed)

1752819042671.png


So... the "bigger" problems i think are the following:

[1]
xxxxxxxxx.starter.handleStart
java.lang.RuntimeException


May be because i don't have starter service... do i need it ? I thought that we must not use starter service. do i need just have it in my code and never use it ? - or what ?
However if not needed - I am not calling it anywhere... why having this error?

[2]
android.app.ActivityThread.generateForegroundServiceDidNotStopInTimeException
android.app.RemoteServiceException$ForegroundServiceDidNotStopInTimeException

at my service have those:
B4X:
Sub Service_Create
    'This is the program entry point.
    'This is a good place to load resources that are not specific to a single activity.
    
    
    Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_NEVER
    If Provider.IsInitialized=False Then Provider.Initialize
    
    xtimer.Initialize("xtimer",20000)
    
    If Main.secureit=True Then
        mytimer.Initialize("mytimer",5000)
    End If
    
End Sub

Sub Service_Start (StartingIntent As Intent)
    
    If onlyonetime=0 Then
    
    StartForegroundWithType("...")

    n.Initialize("default", Application.LabelName, "LOW").AutoCancel(True).SmallIcon(LoadBitmap(File.DirAssets,"icon4app.png"))'.LargeIcon(LoadBitmap(File.DirAssets,"icon2.png"))
    
    n.Build("xxxxxxxxxx", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "tag1",  Main).Notify(1)

        StartServiceAt(Me, DateTime.Now +  30 * DateTime.TicksPerMinute, True)

        xtimer.Enabled=True
        If Main.secureit=True Then
            mytimer.Enabled=True
        End If


    End If
    
    onlyonetime=1

End Sub


Sub StartForegroundWithType(notif As Notification)
    Dim oContext As JavaObject
    Dim serviceType As Int = 1  
    
    Dim RunningPhone As Phone
    
    oContext.InitializeContext
    
    If RunningPhone.SdkVersion >= 29 Then
          
        Try
            oContext.RunMethod("startForeground", Array As Object(1, CreateNotification(notif), serviceType))
        Catch
            Log(LastException)
            Service.StartForeground(1, notif)
        End Try
    Else
        Service.StartForeground(1,  CreateNotification(notif))
    End If
End Sub


Sub delayedstops
    
    If Main.isexited=False Then
    Main.isexited=True
    xtimer.Enabled = False
    mytimer.Enabled = False

    ToastMessageShow("my app will close...",True)
    ' Stop manual foreground mode (and remove the notification)
    Service.StopForeground(1)
    Sleep(500)
    ' Cancel any future scheduled starts
    CancelScheduledService(Me)
    Sleep(500)

    
    CallSubDelayed2(Main, "ServiceStopped", "myService")
    End If
    
End Sub


Sub Service_Destroy
    If Main.isexited=False Then CallSubDelayed(Me,"delayedstops")
End Sub



'Return true to allow the OS default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return False
End Sub

Private Sub Service_Timeout(Params As Map)
    Service.StopForeground(1)
End Sub

Is it something that i can do for stopping the service at the specific time and auto start it - to continue my work ?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
It doesn't look related to the starter service.

About this error generateForegroundServiceDidNotStopInTimeException:
If you are starting a foreground service with the shortService foreground type then you must handle the timeout event and call StopForeground:
B4X:
Private Sub Service_Timeout(Params As Map)
    Service.StopForeground(NotificationId)
End Sub
 
Upvote 0

Magma

Expert
Licensed User
Longtime User
It doesn't look related to the starter service.

About this error generateForegroundServiceDidNotStopInTimeException:
If you are starting a foreground service with the shortService foreground type then you must handle the timeout event and call StopForeground:
B4X:
Private Sub Service_Timeout(Params As Map)
    Service.StopForeground(NotificationId)
End Sub
but already doing that...

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


May be my problem is that starting the service many times ????
in Service_Start two times: 1) StartForegroundWithType, 2) StartServiceAt(Me, DateTime.Now + 30 * DateTime.TicksPerMinute, True)

what i must keep ?

is this needed ? Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_NEVER
/ because of "new" shortservices
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
You don't need StartForegroundWithType. Such a method is only needed if you start the service with different foreground types.
It looks like the ForegroundServiceDidNotStopInTimeException can happen if you stop the service before the notification became visible. Make sure not to stop the service too soon.

is this needed ? Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_NEVER
Yes.

May be my problem is that starting the service many times ????
in Service_Start two times: 1) StartForegroundWithType, 2) StartServiceAt(Me, DateTime.Now + 30 * DateTime.TicksPerMinute, True)
I don't think that it is related.
 
Upvote 0
Top