Does calling stopService() stops all awaiting service intent

て烟熏妆下的殇ゞ 提交于 2020-01-17 06:51:43

问题


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);


来源:https://stackoverflow.com/questions/37469409/does-calling-stopservice-stops-all-awaiting-service-intent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!