Android Question Check if Service has been Created and Started

aaronk

Well-Known Member
Licensed User
Hi,

I have a service (called MyService) and I have many Activitys.

One of the Activitys will create & start the service, but I want to be able to use all my Activitys to call a sub within it.

I have used the following code but there might time to time where the service might not be created yet but my code still wants to call the sub in the service.

B4X:
CallSub2("MyService","TheSub")
Lets say the above code was sent from Activity5 but Activity1 might be the Activity that actually creates/starts the service. There might be a time where Activity1 is never opened so the service is never created.

Calling the above call then fails since the service is not created/started.

Is there a way to check to see if the service has started or been created from a Activity so that I know to call the sub or not?
 

mangojack

Well-Known Member
Licensed User
B4X:
If IsPaused(service) ' it means that the service is NOT running.
Or you could create a process_global flag /variable in activity1 ,which would be set immediately after starting the service.
other activities could check this flag .
 
Last edited:

mangojack

Well-Known Member
Licensed User
maybe he wants to allow all activities to start the service.
i'm reading it as ...
so that I know to call the sub or not?


In this case, the global variable (flag) should be in a Code Module.
yeah .. if activity1 not started ,the variable is useless... Or in the service itself then ?

*Edit* noo if the flag is in activity1 and it is not set to true , then you know the service is not started.. o_O

more simple .. just use
B4X:
If IsPaused(service)
 
Last edited:

udg

Expert
Licensed User
CallsubDelayed should start the service before calling the sub.
Or a simple test like the one in post #2 complete with code to start the service from any Activity.

Just my 2cent.
 
Last edited:

LucaMs

Expert
Licensed User
i'm reading it as ...





yeah .. if activity1 not started ,the variable is useless... Or in the service itself then ?

Act1, Act2, Atc3... MyModule.ServiceIsOn

Act1
If MyModule.ServiceIsOn
callsub
else
MyModule.ServiceIsOn = True
StartService
end if

Act2
If MyModule.ServiceIsOn
callsub
else
MyModule.ServiceIsOn = True
StartService
end if

Act2
If MyModule.ServiceIsOn
callsub
else
MyModule.ServiceIsOn = True
StartService
end if
 

mangojack

Well-Known Member
Licensed User
Very good Lucas .. two bob Aaronk just wants to know of service is running ..;)
 

LucaMs

Expert
Licensed User
I found this:
B4X:
private boolean isMyServiceRunning() {
    ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
    for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
        if ("com.example.MyService".equals(service.service.getClassName())) {
             return true;
        }
    }
    return false;
}

If someone (a name at random: Erel :D) wants to translate it into a B4A function...

Many thanks
 

aaronk

Well-Known Member
Licensed User
Hi All,
Thanks for all your help.. I think CallSubDelayed is going to be the best option here and will try when I get home .
 
Top