Check if service is running or not

57644 views
Skip to first unread message

Mark Wyszomierski

unread,
Dec 26, 2007, 10:07:31 PM12/26/07
to Android Developers
Hi,

Is it possible to check if an instance of a process is already running
in the OS? For example I don't want to try binding to my remote
service if it's not already running:

if (!bindService(new Intent(ServiceTestActivity.this,
ServiceTestService.class),
null, mConnection, 0))//
Context.BIND_AUTO_CREATE);
{
PrintNotification(0, "Unable to bind to remote service, make
sure it has been started before attempting to bind!");
}

so I don't want to auto create it via bindService(). How can we check
if the service is already running or not?

Thanks,
Mark

ho...@helloandroid.com

unread,
Dec 26, 2007, 11:48:11 PM12/26/07
to Android Developers
You could track the state of the service using some kind of persistant
variable (ie., preferences or sql lite) that you can set on onCreate()
and onDestroy() in your service, or you could try to see if you can
see the process running using something like this:
http://davanum.wordpress.com/2007/12/18/android-task-manager-primitive-prototype/

--

Zach Hobbs
HelloAndroid.com
Android OS news, tutorials, downloads

Mark Wyszomierski

unread,
Dec 27, 2007, 3:22:09 PM12/27/07
to Android Developers
Thanks a lot Zach, that link looks like exactly what I need. I've also
seen helloandroid.com, really nice,

Mark

On Dec 26, 11:48 pm, "ho...@helloandroid.com" <ho...@helloandroid.com>
wrote:
> You could track the state of the service using some kind of persistant
> variable (ie., preferences or sql lite) that you can set on onCreate()
> and onDestroy() in your service, or you could try to see if you can
> see the process running using something like this:http://davanum.wordpress.com/2007/12/18/android-task-manager-primitiv...
>
> --
>
> Zach Hobbs
> HelloAndroid.com
> Android OS news, tutorials, downloads
>
> On Dec 26, 10:07 pm, Mark Wyszomierski <mar...@gmail.com> wrote:
>
>
>
> > Hi,
>
> > Is it possible to check if an instance of a process is already running
> > in the OS? For example I don't want to try binding to my remote
> > service if it's not already running:
>
> > if (!bindService(new Intent(ServiceTestActivity.this,
> >                              ServiceTestService.class),
> >                              null, mConnection, 0))//
> > Context.BIND_AUTO_CREATE);
> > {
> >         PrintNotification(0, "Unable to bind to remote service, make
> > sure it has been started before attempting to bind!");
>
> > }
>
> > so I don't want to auto create it via bindService(). How can we check
> > if the service is already running or not?
>
> > Thanks,
> > Mark- Hide quoted text -
>
> - Show quoted text -

hackbod

unread,
Dec 27, 2007, 11:20:57 PM12/27/07
to Android Developers
On Dec 26, 8:48 pm, "ho...@helloandroid.com" <ho...@helloandroid.com>
wrote:
> You could track the state of the service using some kind of persistant
> variable (ie., preferences or sql lite) that you can set on onCreate()
> and onDestroy() in your service, or you could try to see if you can
> see the process running using something like this:http://davanum.wordpress.com/2007/12/18/android-task-manager-primitiv...

It would be best to not depend on knowing the name of the process that
your service is running in. You can bind to a service without causing
it to start; by doing so, you will be told as the service is created
and destroyed.

If your client and server code is part of the same .apk and you are
binding to the service with a concrete Intent (one that specifies the
exact service class), then you can simply have your service set a
global variable when it is running that your client can check.

We deliberately don't have an API to check whether a service is
running because, nearly without fail, when you want to do something
like that you end up with race conditions in your code.

Tom Rutchik

unread,
May 8, 2018, 3:11:14 PM5/8/18
to Android Developers
A comment about the statement:
-----------------------------
We deliberately don't have an API to check whether a service is
running because, nearly without fail, when you want to do something
like that you end up with race conditions in your code.
----------------------------
The problem is that if the service is already running and you issue a call to bind to that service, I didn't get a callback to my ServiceConnection onServiceConnected method.  In my case, I was using a semaphore to indicate that the service was actually connected before making a call on that service.  Since it's not called, the semaphore is not released and code doing the semaphore acquire will not proceed. That's an example of why one sometimes one might want to check if the service is already running. I don't know the developer's team rationale, but it's probably bad form that sometimes you don't get a callback to the onServiceConnected when you issue a call to bind to a service; even if that service was already running.  I used the semaphore because, without it I noticed a race condition between using the service and the service actual being available from the time after I issued a call to bind to that service.  So you solve one problem but create another.  Something to think about.
Reply all
Reply to author
Forward
0 new messages