Android Question StartServiceAtExact / Receiver or Service ?

Magma

Expert
Licensed User
Longtime User
Good day to everyone..

Exploring the differences of services and receivers... also the real need of receiver...

(in the example, Erel using StartServiceAtExact with a receiver, i imagine - that if need to do a job at the exact time will add something in receiver code)

but as i understand StartServiceAtExact can use also a Service... right ?

Asking every AI, taking as result that Services (if you take permission from Google running shortservice) - will always run at exact time and the code time execution... can be for some minutes... if there is a need.

As i know the difference of receivers is that not run always in exact time, following rules of devices, and can do very small jobs (time code execution) ~15 secs..

Which exactly is the difference between StartServiceAtExact and StartReceiverAtExact ?


I am taking my last post of this thread: https://www.b4x.com/android/forum/t...for-service-type-datasync.168875/post-1036388
the following workis in Android 12 version, but at 12+ ?? seems need more permissions..
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

At the end... I am understanding that the real need... Receiver or Service... need also: android.permission.SYSTEM_ALERT_WINDOW... if need to wake up the device and do a work...
but is that really working in new devices?

As for receivers... let's take the snippet of Erel
where exactly put the DoWorkLoop and is that will wake up the device (need to use phone library to do that as i understand) ?

just thoughts, searching for a real working solution for any version...
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Don't try to start services while your app is in the background. It no longer works.

 
Upvote 0

Magma

Expert
Licensed User
Longtime User
Upvote 0
Top