Does calling stopService() stops all awaiting serv

2019-09-06 18:25发布

问题:

In my application, I'm starting a Service multiple times even when it's running. Its a service that is perpetually running.

My question here is, when I call stopService(), will it stop the current running Service and ALSO clear the stack of the waiting intent? Because I want to be sure it is not running anymore.

回答1:

I dont stop my Service, it is STICKY Service. When starting app i am controlling the service is running or not like :

public static boolean isMyServiceRunning(Class<?> serviceClass,Context mContext) {
    ActivityManager manager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
    for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
        if (serviceClass.getName().equals(service.service.getClassName())) {
            return true;
        }
    }
    return false;
}

Usage :

if(!isMyServiceRunning(MyService.class, getApplicationContext())){
       startService(new Intent(this, MyService.class));
}

But when i logout my user i am stopping service like :

Intent serviceIntent=new Intent(mContext,MyService.class);
mContext.getApplicationContext().stopService(serviceIntent);