Checking if service is running

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

Checking if service is running

Postby lokiloki » Sat May 01, 2010 2:36 am

Hi. I'm developing an Android application which consists of 2 parts: the service and activity. The activity, in fact, is just 2 buttons: start service and stop service. In order to decide which button needs to be disabled, I need to check if service is running or not. When service is running, "start service" must be disabled, otherwise it's "stop service". So, the main problem here is that Android doesn't have a "pure" solution to check if service is running or not. There are lots of conversation all over the net, and as far as I know for the moment, it's true.

The method I'm currently using seems to be odd. I've implemented an extra abstraction layer which is aimed to manage service's inner state. Service could either be 'active' or 'inactive'. The interface provides the method isActive() which tells me the service's state. When my activity is ran, first, I run my service using startService(), and then I do bindService(). So, the service is always running. When user leaves an activity, I call isActive() to check if service is active or not, and if it's not, I call stopService(). So, if the service is active, it continues its life. When the activity next time started, I do the same and this way "solves" the problem. But for me it looks to odd:

1. Too much code to solve such a simple problem
2. In fact, my activity is the only one who needs this service, so Binder interface seems to be too tough solution here.

After investigating for a while, I have an idea of using static methods instead of Binder and call these methods directly from my activity. As far as I know, by default both activity and service are ran in a single process (while they both are in the same .apk), so this should work. The question I'd like to ask you is: what do you think about? Do you have any experience of this kind? I'm just not sure, since there are no "good practices" in Android programming yet.

Thanks in advance
Posts: 3
Joined: Sat May 01, 2010 2:19 am


Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 9 guests