本文介绍了调用stopService()是否会停止所有正在等待的服务意图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,即使服务正在运行,我也会多次启动它.它是永久运行的服务.

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

我的问题是,当我呼叫stopService()时,它会停止当前正在运行的Service并清除等待意图的堆栈吗?因为我想确保它不再运行.

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.

推荐答案

我没有停止我的Service,它是STICKY Service.启动应用程序时,我正在控制服务是否运行:

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

用法:

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

这篇关于调用stopService()是否会停止所有正在等待的服务意图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 17:33