Android Question B4A - Background Service + Timer

Ross Woodward

Member
Licensed User
Longtime User
Hi,

Hope you can help me.
I'm looking at the "Service Module" and "Timers".

I'm not sure if I'm doing this right, so i'm hoping you can help.

The Goal;
- Have background service that will check every 2 mins
- It will check against internal DB and if has match, it will post it to the web service.
- if not, it will check again in 2 minutes.

So i created the service module as explained in the tutorial, and tried to create single timer, however I can't see if it's working as - log, toastmsg or notification won't display.

So i have scrapped the code and started with simple timer.
Please see my code:

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    
    Dim StartTimer As Timer
    Dim x As Int = 0
    Dim Notification1 As Notification
End Sub

Sub Service_Create
    
    StartTimer.Initialize("HttpCall", 1000) '900000 = 1000*60*15(the 15 = 15 minutes)
    Notification1.Initialize
    Notification1.Icon ="icon"
    Notification1.Vibrate =True
End Sub

Sub Service_Start (StartingIntent As Intent)
    
    StartTimer.Enabled = True
    Notification1.SetInfo("Starting to count", "Testing",Main)
    Service.StartForeground(1, Notification1)
End Sub

Sub HttpCall_Tick
    
    ToastMessageShow(x, True)
    
    x = x  + 1
    
    If x = 60 Then
        StartTimer.Enabled = False
        Notification1.SetInfo("Stopped Counting", "done", Main)
        Service.StopForeground(1)
        ToastMessageShow("Done", True)   
    End If
End Sub

Thanks for your help.
 

Ross Woodward

Member
Licensed User
Longtime User
Found it! -

I didn't start the background service. =

In the main activity on the "Activity Create" =

StartService(ServiceName)

Thanks
 
Upvote 0
Top