Android Question CallSub when Activity Paused

Discussion in 'Android Questions' started by imgsimonebiliato, Jun 26, 2015.

  1. imgsimonebiliato

    imgsimonebiliato Well-Known Member Licensed User

    Hello,
    I use a service to do a request http to a web server.
    I need to execute this request, even when the user exit from the app.

    How can I do?
     
  2. imgsimonebiliato

    imgsimonebiliato Well-Known Member Licensed User

    I know the usage of CallSubDelayed, but I don't want that the request is sending to waiting queue.
     
  3. thedesolatesoul

    thedesolatesoul Expert Licensed User

    The request will be executing once started even if the user exits the app.
     
  4. imgsimonebiliato

    imgsimonebiliato Well-Known Member Licensed User

    Hello @thedesolatesoul,
    from the log, I've got:

    Code:
    ** Service (screalist) Create **
    ** 
    Service (screalist) Start **
    1
    2
    ** 
    Activity (main) Pause, UserClosed = false **
    3
    4
    5
    sending message 
    to waiting queue (CallSubDelayed - CreateLists)
    But the sub "CreateLists" it isn't execute on the background
     
  5. thedesolatesoul

    thedesolatesoul Expert Licensed User

    Is the Service calling the callsub in the activity?
     
  6. imgsimonebiliato

    imgsimonebiliato Well-Known Member Licensed User

    Yes..
    Here it is the code of the service module:


    Code:
    #Region  Service Attributes
        
    #StartAtBoot: False
    #End Region

    Sub Process_Globals
    Dim Time As Timer
    Dim Counter As Int
    Counter = 
    0
    End Sub

    Sub Service_Create
    Log("--Service Started--")
    Time.Initialize ( 
    "Time"1000 )
    Time.Enabled = 
    True
    End Sub

    Sub Time_Tick
    Counter = Counter + 
    1
    Log(Counter)

    If Counter = 5 Then
        CallSubDelayed(Main, 
    "CreateLists")
        Counter = 
    0
    End If
    End Sub

    Sub Service_Start (StartingIntent As Intent)

    End Sub

    Sub Service_Destroy
    Time.Enabled = 
    False
    End Sub
     
  7. thedesolatesoul

    thedesolatesoul Expert Licensed User

    So what you are saying is that you want to display the activity, even after the user exits and is in the middle of playing a game?
     
  8. imgsimonebiliato

    imgsimonebiliato Well-Known Member Licensed User

    It isn't necessary to display the activity, but only execute the sub
     
  9. thedesolatesoul

    thedesolatesoul Expert Licensed User

    Yes. The activity sub does not exist without the activity.

    If the sub is written in a way that it can be executed without needing the activity then it should be in a code module or a service.
     
  10. imgsimonebiliato

    imgsimonebiliato Well-Known Member Licensed User

    And if it is written in a way that it can't be executed without needing the activity?
     
  11. thedesolatesoul

    thedesolatesoul Expert Licensed User

    Then you actually need to display the activity just to execute the sub.
     
  12. imgsimonebiliato

    imgsimonebiliato Well-Known Member Licensed User

    Ok I understand.
    And, if I want that this service continues his life cycle, even when the user kills it?
     
  13. thedesolatesoul

    thedesolatesoul Expert Licensed User

    The service will stay alive until it has things to do.
    If you really want to force it to never die, you need to make it either a foreground service (no killing) or a sticky service (gets killed but re-spawns).
     
    imgsimonebiliato likes this.
  14. imgsimonebiliato

    imgsimonebiliato Well-Known Member Licensed User

    Please, can you post some example code for a foreground service?
     
  15. DonManfred

    DonManfred Expert Licensed User

  16. imgsimonebiliato

    imgsimonebiliato Well-Known Member Licensed User

    Is it possibile, to start the service, when the user power on the device?
     
  17. thedesolatesoul

    thedesolatesoul Expert Licensed User

    Foreground service is similar to a normal service except you need to create a notification.
    In the ServiceStart you can add Service.StartForeground(0, notification)
     
  18. thedesolatesoul

    thedesolatesoul Expert Licensed User

    When the device is powering down, its too late to start anything.
     
  19. qsrtech

    qsrtech Active Member Licensed User

    I think setting startatboot:true in the service's settings at the very top of the code module should do what you want.
     
  20. thedesolatesoul

    thedesolatesoul Expert Licensed User

    Oops I read that as power 'off' the device
     
Loading...