Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Check if service is running via. bindService

Tags:

What would be the best way to check if an Android Service is running? I am aware of the ActivityManager API, but it seems like the use of the API is not advised for the scenarios similar to mine (source). I am also aware of the possibility of using global/persistent variables to maintain the state of a service.

I have tried to use bindService with flags set to 0, but I got the same problems as the person on the source link (the only exception was, I was trying the bindService with a local service).

The following call

getApplicationContext().bindService(new Intent(getApplicationContext(),
        MyService.class), mServiceConnection, 0);

always returns true, but does not get connected. Is this the expected behaviour? It seems to me bindService should return false if the service is not already running (it is not, I have checked that) or if the BIND_AUTO_CREATE flag is not set (again, it is not).

like image 949
Igor Avatar asked Dec 14 '10 18:12

Igor


People also ask

How can I tell if an Android service is running?

You can do this by making your own Interface where you declare for example " isServiceRunning() ". You can then bind your Activity to your Service, run the method isServiceRunning(), the Service will check for itself if it is running or not and returns a boolean to your Activity.

Does bindService start service?

Binding to a started service As discussed in the Services document, you can create a service that is both started and bound. That is, you can start a service by calling startService() , which allows the service to run indefinitely, and you can also allow a client to bind to the service by calling bindService() .

Which method is called when bindService is used to start a service?

A service is termed as bounded when an application component binds itself with a service by calling bindService() method. To stop the execution of this service, all the components must unbind themselves from the service by using unbindService() method.

How do I find my service instance android?

Use both startService() and bindService() , if needed. How can I get an instance to the service started if I do not use bindService? Either use bindService() with startService() , or use a singleton. By "using a singleton" you mean that I should declare my methods static in the service class?


1 Answers

I have the same issue; seems the best known solution is to create a public static boolean in WhateverService, set to true during onCreate (or onStartCommand, your choice) and false during onDestroy. You can then access it from any other class in the same apk. This may be racey though. There is no API within Android to check if a service is running (without side effects): See http://groups.google.com/group/android-developers/browse_thread/thread/8c4bd731681b8331/bf3ae8ef79cad75d

I suppose the race condition comes from the fact that the OS may kill a remote service (in another process) at any time. Checking a local service (same process), as suggested above, seems race-free to me -- if the OS kills the service, it kills whatever checked its status too.

like image 79
bryanr Avatar answered Oct 11 '22 09:10

bryanr