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

carlos7000

Well-Known Member
Licensed User
Longtime User
I want to create a service that runs every 'n' seconds.

Before it was as simple as

B4X:
Private Sub Service_Start (StartingIntent As Intent)
    ToastMessageShow ("RUNNING...", False)
    StartServiceAt(Me, DateTime.Now + 60 * 1000, True)
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:

B4X:
#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

B4X:
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:

service.png


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

Regards.
 
Last edited:

Xicu

Active Member
Licensed User
Longtime User
I have doubts.:(

1. Add a new service

Would be correct use the starter service?

3. Use a timer to do whatever you like every n seconds.
All other options will not work.

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

B4X:
#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 String) As Boolean
    Return True
End Sub

Sub Service_Destroy

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

Attachments

  • StarterTimer.zip
    405 KB · Views: 433
Upvote 0

Xicu

Active Member
Licensed User
Longtime User
DonManfred, thanks for your response.
3. Use a timer to do whatever you like every n seconds.

All other options will not work.


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