Android Question Service can "start" even though it is already running?

JohnC

Expert
Licensed User
Longtime User
I have the below logging code in a service:

B4X:
Sub Service_Create
   
    LogAppEvent(False,"Timer_Service: Created",False,False)
   
End Sub

Sub Service_Start (StartingIntent As Intent)
   
    LogAppEvent(False,"Timer_Service: Start",False,False)
   
    StartServiceAt(Me, DateTime.Now + 2 * DateTime.TicksPerMinute, True)        'set just in case something goes wrong in the mean time

    Service.StartForeground(nid, CreateNotification("myapp","Starting..."))
   
    If StartingIntent.HasExtra("android.intent.action.BOOT_COMPLETED") = True Then
        LogAppEvent(False,"Timer_Service: Started via Device Booted",False,False)
       
    Else if StartingIntent.HasExtra("android.intent.extra.ALARM_COUNT") = True Then
        LogAppEvent(False,"Timer_Service: Started via Schedule",False,False)
    End If

End Sub

Sub Service_Destroy

    LogAppEvent(False,"Timer_Service: Destroy",False,False)

End Sub

And the logs are reporting this:

B4X:
03/16/20 06:05:43 pm   Timer_Service: Start
03/16/20 06:05:44 pm   Timer_Service: Started via Schedule
03/16/20 06:07:45 pm   Timer_Service: Start
03/16/20 06:07:45 pm   Timer_Service: Started via Schedule
03/16/20 06:09:45 pm   Timer_Service: Start
03/16/20 06:09:46 pm   Timer_Service: Started via Schedule
03/16/20 06:11:48 pm   Timer_Service: Start
03/16/20 06:11:48 pm   Timer_Service: Started via Schedule
03/16/20 06:13:50 pm   Timer_Service: Start
03/16/20 06:13:50 pm   Timer_Service: Started via Schedule
03/16/20 06:15:50 pm   Timer_Service: Start
03/16/20 06:15:50 pm   Timer_Service: Started via Schedule
03/16/20 06:17:50 pm   Timer_Service: Start
03/16/20 06:17:50 pm   Timer_Service: Started via Schedule
03/16/20 06:19:51 pm   Timer_Service: Start
03/16/20 06:19:51 pm   Timer_Service: Started via Schedule
03/16/20 06:21:51 pm   Timer_Service: Start
03/16/20 06:21:51 pm   Timer_Service: Started via Schedule

Since I am not seeing any "Timer_Service: Destroy" entries in the above event list, it looks like the service never stopped or got destroyed.

So, am I understanding this correctly that a service can still "start" again even though it was already running? Meaning that I can think of "Service_Start" sub more like a regular sub - except that it will automatically run just once when the service is created. But I can then call this sub again and again as needed?

And if I wanted to rerun the Service_Start from a code or activity module, which is the preferred way:

StartService(Timer)

or

CallSubDelayed(Timer,"service_start")

And if the preferred way is "StartService()", will that still run the "Service_Start" sub even if the service was already running?
 
Last edited:

JohnC

Expert
Licensed User
Longtime User
You should call StartService if you want Service_Start to run again.

So "StartService" will run the Service_Start routine again even if the service was NOT destroyed yet?
 
Upvote 0
Top