开发者

How to tell if my unbound service is running?

开发者 https://www.devze.com 2023-04-08 13:03 出处:网络
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 w

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 somewhe开发者_运维百科re 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?


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号