Android Question Timer in Starter

Dianzoa

Active Member
Licensed User
Hello Friends!
I have a Timer in the Starter Service, to watch for changes in a remote database, even if the app is minimized o in background the starter must check for the database changes. It works for a short period of time, then it crush. Heres is the image of the log error.
EDIT: Actually the title has to be: " Calling httpjobs in Starter Service"
B4X:
Sub Service_Start (StartingIntent As Intent)
 
    If gpsClient.GPSEnabled=True Then
     
    '        CallSub(Me,"StartGps")
        timer2.Initialize("timer2",20000)
        timer2.Enabled = True
'        gpsClient.Start(0, 0)
'        'timer_gps.Initialize(timer_gps_tick, 35000)
'        'timer_gps.Enabled = True
'        'ProgressDialogShow("Waiting for GPS location")
    End If
End Sub

And I check the database in the timertick event
B4X:
Dim ServerUrl As String
    ServerUrl = "someurl.aspx?id=" & Main.idrepartidor
    Log(ServerUrl)
    Dim job As HttpJob
    'Msgbox(ServerUrl,"")
   
    job.Initialize("", Me)
    job.PostString(ServerUrl,"")
    Wait For (job) JobDone(j As HttpJob)
    If job.Success Then
        Dim parser As JSONParser
        Dim response As String = job.GetString
        'Msgbox(response,"")
        parser.Initialize(response)
        Dim rows As List
        rows = parser.NextArray
        If rows.Size > 0 Then
        'Msgbox("trae filas","")
            For i = 0 To rows.Size - 1
                'SendMessage(str(Main.idrepartidor), "Nuevo Pedido", Main.nombre_cliente As String)
                'Log("Rows #" & i)
                Dim m As Map
                m = rows.Get(i)
                'Msgbox(Main.idrepartidor,"repartidor")
                'Main.idrepartidor = m.Get("idrepartidor")
                nombre_cliente = m.Get("nombre_solicitante")
                           
                Select m.Get("estado")
                    Case "P "
                        estado = "Pendiente"
                                   
                        If ya_notifique = 0 Then
                                       
                            Dim p As Phone
                            'Msgbox(p.SdkVersion,"")
                            If p.SdkVersion >= 21 Then
                                Dim nb As NotificationBuilder
                                nb.Initialize
                                           
                                nb.ContentTitle = "Nuevo Pedido"
                                nb.ContentText = "Nuevo Pedido de " & nombre_cliente
                                nb.AutoCancel = True
                                If Main.idrepartidor > 0 Then
                                    nb.setActivity(interfaz_inicio)
                                Else
                                    nb.setActivity(Main)
                                End If
                                nb.SmallIcon = "ducati"
                                ya_notifique = 1
                                Main.notifico = 1
                                'nb.Notify(1)
                                If p.SdkVersion >= 26 Then
                                    Dim ctxt As JavaObject
                                    ctxt.InitializeContext
                                    Dim manager As JavaObject
                                    manager.InitializeStatic("android.app.NotificationManager")
                                    Dim Channel As JavaObject
                                    Dim importance As String
                                    importance = "IMPORTANCE_HIGH"
                                    Dim ChannelVisibleName As String = Application.LabelName
                                    Channel.InitializeNewInstance("android.app.NotificationChannel", _
                                                   Array("MyChannelId1", ChannelVisibleName,manager.GetField(importance)))
                                    manager = ctxt.RunMethod("getSystemService", Array("notification"))
                                    manager.RunMethod("createNotificationChannel", Array(Channel))
                                    Dim jo As JavaObject = nb
                                    jo.RunMethod("setChannelId", Array("MyChannelId1"))
                                    ya_notifique = 1
                                    nb.Notify(1)
                                    Return
                                Else
                                    nb.Notify(1)
                                End If
                                   
                            Else
                                Dim n As Notification
                                n.Initialize
                                n.AutoCancel = True
                                n.Icon = "ducati"
                                If Main.idrepartidor > 0 Then
                                    n.SetInfo("EnviaPy","Nuevo pedido de " & nombre_cliente, interfaz_inicio)
                                Else
                                    n.SetInfo("EnviaPy","Nuevo pedido de " & nombre_cliente, Main)
                                End If
                                   
                                ya_notifique = 1
                                Main.notifico = 1
                                n.Notify(1)
                            End If
                       
                        End If
                        'End If
                    Case "R "
                               
                    Case "T "
                               
                End Select
                       
                           
            Next
        End If
    End If
End If
End Sub

Any tips?
Regards, Diego
 

Attachments

  • timer_starter_error.png
    timer_starter_error.png
    11.8 KB · Views: 304
Last edited:

musaso

Active Member
Licensed User
In Android 8.1 the test service always remains active and the notification as well.
How can the test service be terminated?
 
Upvote 0

Semen Matusovskiy

Well-Known Member
Licensed User
How can the test service be terminated?
If you talk about my sample, you need to correct Service_Destroy event
Instead of simple
B4X:
StartService (Me)
use
B4X:
If DoNotRestart = False Then StartService (Me)
where DoNotRestart is process global variable

When you want to kill the service Test
B4X:
DoNotRestart = True
StopService (Test)

About notifications. I used different ways
If API >= 23 I use Erel's NB6, otherwise - NotificationBuilder.
 
Upvote 0

Dianzoa

Active Member
Licensed User
I still have problems, my app does not keeps awake, I will debug tomorrow, and watch for an error in code , maybe is my fault, and not that android keeps destroying my timer and my global variables. The thing is that when I have the phone connected to the computer through b4a bridge it works much more time than when it's running the app itself in the phone. The app installed in tge phone dies after just a few minutes if the phone is "sleeping" :(
 
Upvote 0

Dianzoa

Active Member
Licensed User
UPDATE: I checked that the service stays alive for many minutes, possibly if you do not touch the phone for 1 hour or something like that, the App dies. I could not find the real reason still. But I think that is enough for me, since it's very unlikely that the phone get out of activity for such a long time when people are working.
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
UPDATE: I checked that the service stays alive for many minutes, possibly if you do not touch the phone for 1 hour or something like that, the App dies. I could not find the real reason still. But I think that is enough for me, since it's very unlikely that the phone get out of activity for such a long time when people are working.
I am facing the same situation, it seems that there's no way around it, if you are using any Services that you want to run in the background the OS will destroy the service, i have also confirmed that if you run the app while connected through the USB cable the app will run for ever, but if you disconnect it and let it run then the service will get destroyed right away.

Walter
 
Upvote 0
Top