我想检查服务是否正在运行 我写了这段代码
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,此)
如果我检查一个班级的服务工作,但是如果我检查来自不同班级的同一服务,那么它的检查不起作用
答案 0 :(得分:33)
1。)在Activity类中:
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;
}
答案 1 :(得分:28)
为什么不让服务本身解决这个问题?
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);
}
}
答案 2 :(得分: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;
}
答案 3 :(得分:0)
检查服务是否正在运行
如果我的情况: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;
}
答案 4 :(得分:0)
您可以在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 }
}