Android Question my app in background with module service.

fifiddu70

Well-Known Member
Licensed User
Longtime User
Hi everyone, I have a problem, my app must perform background monitoring of numbers sent from another device to a host on my website, my app every 3 seconds queries the website displaying one of the 4 numbers in the my website folder, as soon as one of these numbers matches the number stored in the app, it should wake up from the background and play alerting even to the corresponding number, everything seems to work perfectly for the first few minutes even in the background, but after a while seems to be no longer active as if it were killed, yet the notification is always present on the phone, if I click on the notification, the app reopens regularly but no longer alerts me if one of the numbers matches the one stored in my app. Where I'm wrong and above all how do I get the app always awake in the background, I'll post the service module to see if I'm wrong.
B4X:
#Region  Service Attributes
    #StartAtBoot: True
    #StartCommandReturnValue: android.app.Service.START_STICKY
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    
    
    Dim N As Notification
    Dim hc As     OkHttpClient
    Dim testo As String
    Dim timer1 As Timer
    Dim timer2 As Timer
    Dim timer3 As Timer
    Dim timer4 As Timer
    Dim risultatoA As String
    Dim risultatoB As String
    Dim risultatoC As String
    Dim risultatoD As String
    
End Sub

Sub Service_Create
    n.Initialize
    n.Icon = "icon"
    n.Sound = False
    n.Vibrate = False
    n.Light = False
    n.OnGoingEvent=True
    n.SetInfo("Turn And Go","Numero prenotato " &  Main.chiamataA & " " & Main.chiamataB & " " & Main.chiamataC & " " & Main.chiamataD ,Main)
    n.Notify(1)


    hc.Initialize("hc")
    timer1.Initialize("timer1",3000)
    timer2.Initialize("timer2",3000)
    timer3.Initialize("timer3",3000)
    timer4.Initialize("timer4",3000)
    
    timer1.Enabled=True
    timer2.Enabled=True
    timer3.Enabled=True
    timer4.Enabled=True
    
    
End Sub

Sub Service_Start (StartingIntent As Intent)
    
    Service.StartForeground(1,n)   
    StartServiceAt(Me, DateTime.Now + 30 * DateTime.TicksPerMinute, True)
    risultatoA = Main.chiamataA
    risultatoB = Main.chiamataB
    risultatoC = Main.chiamataC
    risultatoD = Main.chiamataD

End Sub




Sub timer1_Tick
    Log("sono sulla sub timer1 di Starter")
    Dim job As HttpJob
    job.Initialize("Job", Me)
    job.Download("http://www.xxxxx.xxx/xxxxxx/"& Main.linea & "/NumeroA.txt")
    
End Sub

Sub timer2_Tick
    Log("sono sulla sub timer2 di Starter")
    Dim job As HttpJob
    job.Initialize("Job", Me)
    job.Download("http://www.xxxxx.xxx/xxxxxx/"& Main.linea & "/NumeroB.txt")
        
End Sub

Sub timer3_Tick
    Log("sono sulla sub timer3")
    Dim job As HttpJob
    job.Initialize("Job", Me)
    job.Download("http://www.xxxxx.xxx/xxxxxx/"& Main.linea & "/NumeroC.txt")
        
End Sub

Sub timer4_Tick
    Log("sono sulla sub timer4")
    Dim job As HttpJob
    job.Initialize("Job", Me)
    job.Download("http://www.siciliashop.xxx/xxxxxx/"& Main.linea & "/NumeroD.txt")
        
End Sub
Sub JobDone (job As HttpJob)
    
    Log("JobName = " & job.JobName & ", Success = " & job.Success)
    If job.Success = True Then
        Select job.JobName
            Case "Job" , "job2"
                'print the result to the logs
                
                testo =job.GetString
                Log(job.GetString)
                If testo.StartsWith("A") Then
                    
                    Log("su starter nel job " & risultatoA)
                    If  job.GetString = risultatoA  Then
                        Log("ok result")
                        
                        StartActivity(avviso)
                        
                    End If
                else if testo.StartsWith("B") Then
                    
                    If  job.GetString = risultatoB  Then
                        Log("ok result")
                        
                        StartActivity(avviso)
                    End If
                    
                else if testo.StartsWith("C") Then
                    
                    If  job.GetString = risultatoC  Then
                        Log("ok result")
                        
                        StartActivity(avviso)
                    End If
                    
                else if testo.StartsWith("D") Then
                    
                    If  job.GetString = risultatoD  Then
                        Log("ok result")
                        
                        StartActivity(avviso)
                    End If
                End If
        
            Case "Job3"
                'show the downloaded image
                'Activity.SetBackgroundImage(job.GetBitmap)
        End Select
    Else
        Log("Error: " & job.ErrorMessage)
        'ToastMessageShow("Error: " & job.ErrorMessage, True)
    End If
    job.Release
    
End Sub
Sub testo_TextChanged (Old As String, New As String)
    If testo = Old Then
        
    Else
        Log("vai sulla sub Creafile")
    End If
End Sub
Sub Service_Destroy
    n.Cancel(1)
End Sub

perchè non rimane attivo come

please help me, this important for me.
 

DonManfred

Expert
Licensed User
Longtime User
`Some notes:
Dim hc As OkHttpClient
- Dont use the OkHttpClient directly. Sounds like an mistake to use it.
timer1.Initialize("timer1",3000)
timer2.Initialize(
"timer2",3000)
timer3.Initialize(
"timer3",3000)
timer4.Initialize(
"timer4",3000)
- Instead of 4 Jobs i would create a php on the serverside which is able to respond all 4 Values with one Call. Only one timer needed, only one call to webside.

More robust solution:
Do not use timers at all. No need for foreground services.
- Create a b4j server app which regularly checks the files. Best is running the b4j server app on the same server.
The serverapp checks all files regularly using a timer. I would not use 4 timers. ONE is enough. In timer tick read all 4 values...

It the b4j app detect the right value it sends a FCM pushnotification to the app.
The app get wake up by this notification and play the audio or whatever you want to do.

Edit to add:
in time of the availability of sleep and wait for i would not use a timer at all.
 
Last edited:
Upvote 0

fifiddu70

Well-Known Member
Licensed User
Longtime User
`Some notes:

- Dont use the OkHttpClient directly. Sounds like an mistake to use it.

- Instead of 4 Jobs i would create a php on the serverside which is able to respond all 4 Values with one Call. Only one timer needed, only one call to webside.

More robust solution:
Do not use timers at all. No need for foreground services.
- Create a b4j server app which regularly checks the files. Best is running the b4j server app on the same server.
The serverapp checks all files regularly using a timer. I would not use 4 timers. ONE is enough. In timer tick read all 4 values...

It the b4j app detect the right value it sends a FCM pushnotification to the app.
The app get wake up by this notification and play the audio or whatever you want to do.

Edit to add:
in time of the availability of sleep and wait for i would not use a timer at all.

sorry DonManfred, how can I use b4j? I will explain to you better what I have realized, it is an app eliminacode, composed in 3 parts, one installed in a smartphone that acts as remote control to 4 types of numbers Number A, Number B, Number C, Number D, this remote control sends on my web site a number, in my website there is a html page that allows through a php file to generate a text file with the number transmitted by the remote control, at the same time the number transmitted by the remote control is also transmitted in a tablet that displays this number, then there is another app that has the purpose of displaying the 4 letters with transmitted number, if one of these 4 numbers transmitted is equal to the number stored in the app then performs a sound or an alert, so I don't understand the use of b4j.
 
Upvote 0
Top