15

l want to check if a service is running l wrote this code

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

and use it startServiceOrNo.startServiceIfItsNotRuning(OfflineChopsMonitor.class,this)

if l check service from one class its work, but if l check same service from different class, its check don't work

Benjamin Schwalb
  • 1,124
  • 11
  • 31
user2542715
  • 185
  • 2
  • 4
  • 10

5 Answers5

36

1.) In Activity class:

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.) In Non-Activity class (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;
    }
Maddy Sharma
  • 4,870
  • 2
  • 34
  • 40
34

Why don't you let the Service itself figure that out?

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);
    }
}
jfs
  • 16,758
  • 13
  • 62
  • 88
  • 3
    But how do you get access to your service object from your intent? – pypmannetjies Apr 02 '14 at 22:27
  • 6
    How would you use this from other Activities/Fragments to check whether the Service is running? – IgorGanapolsky Mar 03 '15 at 16:24
  • 4
    @IgorGanapolsky you wouldn't – Tim Oct 09 '15 at 13:04
  • 2
    Is it not necessary to use a static variable? – JCarlosR Sep 04 '16 at 03:57
  • Good solution, also relevant for the scenario when the service suddenly crashes (`onDestroy` not being called), so explicitly changing the value back to `false` inside `onDestroy` method is optional. – Eido95 Nov 13 '16 at 15:00
  • but it is already starting the same service, right ? but you are not doing anything if it is already running. but you start one again anyway, right ? – Adnan Bal Nov 21 '17 at 12:48
  • 3
    useless answer. what about when service dies? – user155 Apr 26 '19 at 09:21
  • Upvote. Remember the old school fact, the service which has already been started won't call the oncreate when the startservice is called the next time, therefore, the comment // do something is the safe place here. – Prateek Nov 04 '19 at 09:39
  • one solution which I found at SO, exclude this deprecated one `getRunningServices` everywhere... – danyapd Jul 20 '20 at 21:28
10

I use following from inside an 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;
}
Elton da Costa
  • 1,279
  • 16
  • 27
2

You can use this method in 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 }
}
Kourosh
  • 2,239
  • 13
  • 18
1

check if the service is running or not if my case: OpenVPNService.class is my service 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;
    }