6

I am running foreground services in my app and I need to deremine if they running in my activity. From my searches I found that this is the option:

private fun isMyServiceRunning(serviceClass: Class<*>): Boolean {
    val manager = getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
    for (service in manager.getRunningServices(Int.MAX_VALUE)) {
        if (serviceClass.name == service.service.className) {
            return true
        }
    }
    return false
}

but:

getRunningServices(Int): is deprecated.

So I am using one of 3 ways.

  1. Binding the service to the activity with onResume. But I think it a bit overkill for something small just to check if service is running.
  2. Make the Intent public and check if its null, but there can be some cases where the intent not null but service not running
  3. Check if the foreground service persistent notification is active, but this is workaround.

What is the most correct way to check if service is running?

Dim
  • 4,527
  • 15
  • 80
  • 139
  • Does this answer your question? [Android - getRunningservices(ActivityManager) deprecated](https://stackoverflow.com/questions/45519439/android-getrunningservicesactivitymanager-deprecated) – emandt Mar 22 '21 at 08:12
  • The answer is @SuppressWarnings("deprecation"), but is this the correct way, just to suppress it? I am trying to use the code as much time as possible. getRunningServices could be removed totally in couple of months. – Dim Mar 22 '21 at 08:35
  • You can use [bindService](https://developer.android.com/reference/android/content/Context#bindService(android.content.Intent,%20android.content.ServiceConnection,%20int)) and check the return value, or listen ServiceConnection callback to distinguish service type, though there may still have some problems. – ininmm Apr 03 '21 at 03:21

3 Answers3

8

Create a static boolean in the Service itself. In onCreate make it true; In onDestroy() make it false;

public class ActivityService extends Service {

public static  boolean IS_ACTIVITY_RUNNING = false;

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onCreate() {
    super.onCreate();
    IS_ACTIVITY_RUNNING = true;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
            if(!IS_ACTIVITY_RUNNING)
                stopSelf();
    return START_STICKY;
}

@Override
public void onDestroy() {
    super.onDestroy();
    IS_ACTIVITY_RUNNING = false;
}}

Now, You can check whether your activiy is running trough boolean ActivityService.IS_ACTIVITY_RUNNING

1

You can create a SharedPreferences in the onCreate method of the Service and append some boolean value in it.

getSharedPreferences( getString( R.string.app_name ) , Context.MODE_PRIVATE )
     .edit()
     .putBoolean( getString( R.string.service_running_status_key ) , true )
     .apply()

Similarly, in the onDestroy method, append false to the same key. You can then use SharedPreferences.getBoolean to check if the Service is running in foreground.

Shubham Panchal
  • 4,061
  • 2
  • 11
  • 36
1

In kotlin This is working for me->

@Suppress("DEPRECATION")
fun <T> Context.isServiceRunning(service: Class<T>): Boolean {
    return (getSystemService(ACTIVITY_SERVICE) as ActivityManager)
        .getRunningServices(Integer.MAX_VALUE)
        .any { it -> it.service.className == service.name }
}

After this you can call the function like ->

context.isServiceRunning(MyService::class.java)
Kunal Kalwar
  • 685
  • 6
  • 20