Android Question Execute a service every 'n' seconds [Solved]

Discussion in 'Android Questions' started by carlos7000, Sep 29, 2018.

  1. carlos7000

    carlos7000 Active Member Licensed User

    I want to create a service that runs every 'n' seconds.

    Before it was as simple as

    Code:
    Private Sub Service_Start (StartingIntent As Intent)
        
    ToastMessageShow ("RUNNING..."False)
        
    StartServiceAt(Me, DateTime.Now + 60 * 1000True)
    End Sub
    The code works, worse in the Log, it always appears in the message The Starter service should never be started from a receiver.

    I already read the tutorials of these links:

    Intent Filters - Intercepting SMS messages in the background
    https://www.b4x.com/android/forum/t...cepting-sms-messages-in-the-background.20103/

    Automatic Foreground Mode:
    https://www.b4x.com/android/forum/threads/automatic-foreground-mode.90546/

    But I guess something has not been clear to me.

    I already know that when the application starts, Android automatically starts the services in AutomaticForegroundMode mode.

    I also understood that it is not necessary to use the StartServiceAt command because the service never ends, (only in rare cases), because Android returns to run the service automatically, in case the service has been closed, because it is not necessary either:

    Code:
    #StartCommandReturnValue: android.app.Service.START_STICKY
    I understood that you have to add the Service.StopAutomaticForeground line to Service_Start, in order to use StartServiceAt.

    Although that way the program works "Good".

    ¿Is not it absurd to do it this way, if the service is always running?

    I wrote the following lines within a service

    Code:
    Private Sub Service_Start (StartingIntent As Intent)
        
    ToastMessageShow ("RUNNING..."False)
        
    Log(">>> " & DateTime.GetSecond(DateTime.Now))
    End Sub
    But the message "RUNNING ..." only appears once.

    Although the program works and I think I follow the instructions correctly, obviously something is wrong because the Log keeps showing an error message

    What bothers me most is the message The Starter service should never be started from a receiver.

    You can see it in the following screenshot:

    [​IMG]

    ¿So, what is the correct way to run a service every 'n' seconds?

    Regards.
     
    Last edited: Sep 29, 2018
  2. Erel

    Erel Administrator Staff Member Licensed User

    1. Add a new service.
    2. Make it a foreground service.
    3. Use a timer to do whatever you like every n seconds.

    All other options will not work.
     
  3. carlos7000

    carlos7000 Active Member Licensed User

    Hi Erel


    It worked very well.

    Thank you very much


    Regards.
     
  4. Xicu

    Xicu Member Licensed User

    I have doubts.:(

    Would be correct use the starter service?

    Are you refering to use a expression like "StartServiceAt(Me, DateTime.Now + 30 * 1000, True)" or the object timer?


    I ask that because I have write this code in starter service that show a toastmessage every 3 seconds with a count, and seems it works well, but I not sure that is a correct way:(

    This is the simple code in the starter service

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

    Sub Process_Globals
        
    'These global variables will be declared once when the application starts.
        'These variables can be accessed from all modules.
        Private Timer1 As Timer
        
    Private Contador As Int
    End Sub

    Sub Service_Create
        
    'This is the program entry point.
        'This is a good place to load resources that are not specific to a single activity.

    End Sub

    Sub Service_Start (StartingIntent As Intent)
        Contador =
    0
        Timer1.Initialize(
    "Timer1"3000)
        Timer1.Enabled = 
    True 'don't forget to enable it

    End Sub

    Sub Service_TaskRemoved
        
    'This event will be raised when the user removes the app from the recent apps list.
    End Sub

    'Return true to allow the OS default exceptions handler to handle the uncaught exception.
    Sub Application_Error (Error As Exception, StackTrace As StringAs Boolean
        
    Return True
    End Sub

    Sub Service_Destroy

    End Sub
    Private Sub Timer1_Tick
        Contador = Contador+
    1
        
    ToastMessageShow(Contador,False)
        Sleep(
    0)
    End Sub
     

    Attached Files:

  5. DonManfred

    DonManfred Expert Licensed User

    no. You should add a new service and make this service a forground service.
     
  6. Xicu

    Xicu Member Licensed User

    DonManfred, thanks for your response.

    Are he refering to use a expression like "StartServiceAt(Me, DateTime.Now + 30 * 1000, True)" or the object timer?
     
  7. DonManfred

    DonManfred Expert Licensed User

    Timer. He wouldn´t write Timer if he does mean StartServiceAt
     
  8. Xicu

    Xicu Member Licensed User

    Ok, thanks!
     
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