Android Question Service is killed in 3-7 minutes, what is wrong?

senat56

Member
Licensed User
Longtime User
Good day dear programmers. (I use Google as a translator, so do not swear strongly) Briefly, there is an oreo and you need a service that will check a certain URL every 20 seconds and cause a certain action. But the current code does not work much time, oreo kills it. What not so please tell me. Thank.
B4X:
#Region  Service Attributes
    #StartAtBoot: True
#End Region

Sub Process_Globals
    Dim pw As PhoneWakeState
    Dim n As Notification
End Sub

Sub Service_Create
    Service.AutomaticForegroundMode=Service.AUTOMATIC_FOREGROUND_ALWAYS
End Sub

Sub Service_Start (StartingIntent As Intent)
    pw.PartialLock
    Service.StopAutomaticForeground
    n.Initialize
    n.SetInfo("Check data", "Run",Main)
    Service.StartForeground(1,n)
   
    Log("run")
    Wait For (check)  Complete (Result As Int)
    StartServiceAt("",DateTime.Now+20*DateTime.TicksPerSecond,True)
    Service.StopAutomaticForeground
   
End Sub

Sub Service_Destroy

End Sub

Private Sub check() As ResumableSub
    Dim j As HttpJob
    j.Initialize("",Me)
    j.Download("http://www.site.com/data.txt")
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        Log("ok")
        Dim str As String=j.GetString
    End If
    j.Release
    Return 1
End Sub
 

Pendrush

Well-Known Member
Licensed User
Longtime User
You can't start service in 20 sec interval, better start service as foreground (as you already do) and use timer inside service, without stopping service.
 
Upvote 0

senat56

Member
Licensed User
Longtime User
You can't start service in 20 sec interval, better start service as foreground (as you already do) and use timer inside service, without stopping service.
if I knew how to do this (can you set an example?

The user can swipe the program window, but the process still works in the background (3-7 minutes - always in different ways)
 
Upvote 0

senat56

Member
Licensed User
Longtime User
Erel, thanks for the advice (you are always on top), and if you do not mind, you can show an example?
 
Upvote 0
Top