1

I have read the other threads about checking if a service is running but this is not working for me.

My situation is that am creating a background web server service but not binding to it because I want it to continue running after the activity ends. The service creates a notification so users can see it is running. The user can stop the service through a button on the Activity.

This is all working fine, except on launch of the activity, I can't determine if the service is already running. Am using the following:

if (isMyServiceRunning() == false)
{
  Intent ws = new Intent(this, WebServerService.class);
  startService(ws);
}

And this

private boolean isMyServiceRunning() {

    String sClassName;

    ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
    for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) 
    {
        sClassName = service.service.getClassName();
        DebugMsg("Service: " + sClassName);

        if (sClassName.contains("com.<mydomain>.webservice"))
        {
              return true;
        }
    }

    return false;
}

I get a list of services running, for both system and 3rd party services. But my service doesn't show up in the list, even though I know it's running. If I go into the phone's Settings -> Applications -> Running Services, I can see it running there.

I read in the documentation somewhere that calling startService on a service that is already running should be ignored. But that isn't the case as I can see in the debugger that both OnCreate and OnStart are being called.

It is important that I do not create a new service each time because the background service may be in the middle of serving a file. The activity does not need to do any communication with the service - only start it if it isn't running and kill it if the user hits a button.

Any idea on why my service is not showing up in the getRunningServices list?

Community
  • 1
  • 1
Gregg Reno
  • 441
  • 4
  • 15

2 Answers2

4

Step #1: Add static boolean isRunning=false to your service.

Step #2: Set isRunning to true in onCreate() of the service.

Step #3: Set isRunning to false in onDestroy() of the service.

Step #4: Examine isRunning to see if the service is running.

I read in the documentation somewhere that calling startService on a service that is already running should be ignored. But that isn't the case as I can see in the debugger that both OnCreate and OnStart are being called.

I am very confident that onCreate() is not called when startService() is invoked on a running service. onStartCommand() (and, hence, onStart() for older services) will be called for every startService() call.

It is important that I do not create a new service each time

Services are natural singletons. There will be precisely 0 or 1 copies of the service in memory. There will never be 2 or more.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thanks for the quick reply, but no luck. Context on isRunning is lost. I set it to true in OnCreate and check the variable the second time I call startService, and it is still false. onCreate and onStart are always being called. I even created separate static counters incremented for every onCreate, onStart and onDestroy. They are always zero after startService is called. Interesting though that onDestroy is never being called. – Gregg Reno Sep 23 '11 at 18:04
  • @Gregg Reno: Then something is seriously messed up with your app. Make sure you don't have `android:process` attributes in your manifest or something that would cause you to have separate processes. – CommonsWare Sep 23 '11 at 18:23
  • Does it matter that I am also using an IntentService to communicate to another server? – Gregg Reno Sep 23 '11 at 18:49
  • Or as an alternative, can I see if there is an active notification (I create it from my service with FLAG_NO_CLEAR)? Seems I can only Notify or Cancel with NotificationManager. – Gregg Reno Sep 23 '11 at 20:04
  • @Gregg Reno: "can I see if there is an active notification" -- not really. – CommonsWare Sep 26 '11 at 10:53
0

Actually, my service is now showing up in the list of services. I'm now thinking that maybe the service name wasn't registered until after restarting the phone, because I didn't make any changes but everything is working now after restarting.

Gregg Reno
  • 441
  • 4
  • 15
  • Uffff.. that is super messy.. How would u be sure that this would not happen when users are running your app? ;( – Ewoks Nov 22 '13 at 09:32