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

Discussion in 'Android Questions' started by senat56, Oct 22, 2018.

Tags:
  1. senat56

    senat56 Member Licensed 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.
    Code:
    #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
     
  2. Pendrush

    Pendrush Well-Known Member Licensed 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.
     
    senat56 likes this.
  3. senat56

    senat56 Member Licensed User

    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)
     
  4. Sandman

    Sandman Well-Known Member Licensed User

    senat56 likes this.
  5. senat56

    senat56 Member Licensed User

  6. Erel

    Erel Administrator Staff Member Licensed User

    1. Don't use StartServiceAt. Use a timer instead.

    2. Don't stop foreground mode.
     
  7. senat56

    senat56 Member Licensed User

    Erel, thanks for the advice (you are always on top), and if you do not mind, you can show an example?
     
  8. Erel

    Erel Administrator Staff Member Licensed User

    Start with:
    Code:
    Sub Service_Create
        
    Service.AutomaticForegroundMode=Service.AUTOMATIC_FOREGROUND_ALWAYS
    pw.PartialLock
    End Sub

    Sub Service_Start (StartingIntent As Intent)
        
        
    Do While True
        
    Log("run")
        
    Wait For (check)  Complete (Result As Int)
        Sleep(
    20000)
        
    Loop   
    End Sub
     
    rogeriosca and senat56 like this.
  9. senat56

    senat56 Member Licensed User

    Thank!
    You are the best!
    You can close!
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice