how to check service is running in background or not

Put your problem here if it does not fit any of the other categories.

how to check service is running in background or not

Postby srinuv28 » Thu Dec 03, 2009 8:48 am

Hi..

anyone help on how to check whether a service is running in background or not..

please provide some examples on this..

Thanks&Regards
Srinu
srinuv28
Developer
Developer
 
Posts: 36
Joined: Wed Nov 04, 2009 4:04 am

Top

Postby Johan Degraeve » Thu Dec 03, 2009 10:49 am

regards,

Johan
Johan Degraeve
Experienced Developer
Experienced Developer
 
Posts: 55
Joined: Tue Oct 27, 2009 1:50 pm
Location: Belgium

Postby padde » Thu Dec 03, 2009 3:53 pm

How does Thread.isAlive() help here?!
Could you provide an example?

I never had to check if a service is running.. but i would try it with the bindservice
method of the context class. If you choose 0 as flag the method returns true if you
successfully connected or false otherwise.

This will only work if you allowed to bind to the service.. i dont know how to check some
foreign service is running or not.
padde
Master Developer
Master Developer
 
Posts: 443
Joined: Wed Apr 08, 2009 4:52 pm

Postby Johan Degraeve » Thu Dec 03, 2009 4:10 pm

I was a bit too fast here. I was assuming that this 'service' would be running in a thread launched by the current activity, so in that case you could hold a reference to that thread and call isAlive().
But you're probably talking about a
Android Service, that's different off course. Maybe you could define somehwere a field which can be locked.

In your calling activity :

private boolean serviceIsRunning = false;

//called by the thread that wants to know if the service is running
public synchronized boolean checkIfRunning() {
synchronize(serviceIsRunning) {
return serviceIsRunning;
}
}

called by the service to change the status to true at start, to false right before finishing
public synchronized setCheckIfRunning(boolean newvalue) {
synchronize(serviceIsRunning) {
serviceIsRunning = newvalue;
}
}

I'm not sure about the syntax, probably some calls to notify() need to be added.
regards,

Johan
Johan Degraeve
Experienced Developer
Experienced Developer
 
Posts: 55
Joined: Tue Oct 27, 2009 1:50 pm
Location: Belgium

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Google [Bot] and 6 guests