Android Question How to start a service from a service or activity and wait...

tigrot

Well-Known Member
Licensed User
Longtime User
Hi everybody,
is there a way to wait for a service initialization completed from a service or activity? I need to be sure that the service is completelly started before going on. I do with a sleep(5000), but there is something like a semaphore to be sure of service started ok? The service interfaces a webservice so I cannot be sure about init times.

Thank you
Mauro
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Code to call the other service:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   CallSubDelayed2(Service1, "StartServiceAndWait", Me)
   Wait For Service_Ready
   Log("Service1 is ready at this point...")
End Sub

Code in the service:
B4X:
Public Sub StartServiceAndWait(Callback As Object)
   CallSubDelayed(Callback, "Service_Ready")
End Sub
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Code to call the other service:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   CallSubDelayed2(Service1, "StartServiceAndWait", Me)
   Wait For Service_Ready
   Log("Service1 is ready at this point...")
End Sub

Code in the service:
B4X:
Public Sub StartServiceAndWait(Callback As Object)
   CallSubDelayed(Callback, "Service_Ready")
End Sub

I really have to study the new feature (resumable routines), since I would never have thought of such a solution.
I would have called a callback routine from the Service_Start event.
In fact, I think I did this before, complicating the code (but I was forced to do so, the new feature did not exist).
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I would have called a callback routine from the Service_Start event.
This is very similar to what I did.

1. The advantage of CallSubDelayed is that we can pass a parameter (callback module in this case).
2. Instead of handling the Service_Ready event in a different sub we use the new Wait For keyword to catch the event in the current sub.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
2. Instead of handling the Service_Ready event in a different sub we use the new Wait For keyword to catch the event in the current sub.

Yes, this is very useful to avoid:
complicating the code

Using the "old" way, I can start the service from "routine A" but then I must continue elsewhere (the routine called by the service).
 
Upvote 0

tigrot

Well-Known Member
Licensed User
Longtime User
This gives a lot of control. Thank you Erel!

Using the "old" way, I can start the service from "routine A" but then I must continue elsewhere (the routine called by the service).
I'm not sure that starting a service whould have suspended the actual one. One day I'll check! Probably the execution will go on till the next return, if you dont't use CallSub(and have already your service started)
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
'm not sure that starting a service whould have suspended the actual one.
Certainly not.

Maybe I explained badly; it is better to use an "old style" example (2 services?):

B4X:
' FirstService
Sub Service_Start
    StartService(SecondService)
    ' I would like to continue here, after SecondService has completed its initialization,
    ' but without the resumable routines this was not possible.
End Sub

Sub ContinueHere
End Sub

'---------------

' SecondService
Sub Service_Start
    CallSubDelayed(FirstService, "ContinueHere")
End Sub

So your code cannot continue immediately after starting SecondService but the flow "jumps" to a different routine, ContinueHere.
 
Upvote 0
Top