15

我想检查一个服务是否正在运行我写了这段代码

public class startServiceOrNo {
public static void startServiceIfItsNotRuning(Class<?> class1, Context context) {
    ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
        if (class1.getName().equals(service.service.getClassName())) {
            Lg.d("servisstart SERVICE ALREADY START" + class1.getName());
            return;
        }
    }
    Lg.d("servisstart SSERVICE NEW SERVIS " + class1.getName());
    context.startService(new Intent(context, class1));
}

并使用它 startServiceOrNo.startServiceIfItsNotRuning(OfflineChopsMonitor.class,this)

如果我检查一个类的服务它的工作,但如果我检查不同类的相同服务,它的检查不起作用

4

5 回答 5

36

1.)在活动类:

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

2.)在非活动类(BroadcastReceiver)中:

private boolean isMyServiceRunning(Class<?> serviceClass,Context context) {
        ActivityManager manager = (ActivityManager)context. getSystemService(Context.ACTIVITY_SERVICE);
        for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
            if (serviceClass.getName().equals(service.service.getClassName())) {
                Log.i("Service already","running");
                return true;
            }
        }
        Log.i("Service not","running");
        return false;
    }
于 2014-06-11T11:52:41.913 回答
34

为什么不让服务本身解决这个问题?

public class MyService extends Service
{
    private boolean mRunning;

    @Override
    public void onCreate()
    {
        super.onCreate();
        mRunning = false;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId)
    {
        if (!mRunning) {
            mRunning = true;
            // do something
        }
        return super.onStartCommand(intent, flags, startId);
    }
}
于 2013-07-11T10:36:20.523 回答
10

我在活动内部使用以下内容:

private boolean isMyServiceRunning(Class<?> serviceClass) {
  ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
  for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
    if (serviceClass.getName().equals(service.service.getClassName())) {
        return true;
    }
  }
  return false;
}
于 2014-08-27T14:02:56.583 回答
2

您可以在 Kotlin 中使用此方法

fun Activity.isServiceRunning(serviceClassName: String): Boolean {
    val manager = getSystemService(ACTIVITY_SERVICE) as ActivityManager
    return manager.getRunningServices(Integer.MAX_VALUE).any { it.service.className == serviceClassName }
}
于 2019-09-17T19:52:14.350 回答
1

检查服务是否正在运行,如果我的情况:OpenVPNService.class是我的服务类

if (isMyServiceRunning(OpenVPNService.class)) {
                success_txt.setVisibility(View.VISIBLE);
            } else {
                success_txt.setVisibility(View.GONE);
            }





 private boolean isMyServiceRunning(Class<?> serviceClass) {
        ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
            if (serviceClass.getName().equals(service.service.getClassName())) {
                return true;
            }
        }
        return false;
    }
于 2019-02-19T07:32:18.890 回答