Android: How to determine if IntentService is running?

Ruchit Shah picture Ruchit Shah · Feb 3, 2015 · Viewed 11.2k times · Source

I have an activity for upload from which I am calling Intent service. In there I am handling the API request call.

I want an activity to know whether the service is running or not, to show an uploading tag.

I tried following to determine if the service is running:

public void startUploadServiceTask() {
    if (Util.isNetworkAvailable(mContext)) {

        if (!isMyServiceRunning(UploadDriveService.class)) {

                startService(new Intent(mContext,
                        UploadService.class));

            }
        } else {
            Toast.makeText(mContext,
                    "Service is already running.", Toast.LENGTH_SHORT)
                    .show();
        }
    } else {
        Toast.makeText(mContext,
                getString(R.string.please_check_internet_connection),
                Toast.LENGTH_SHORT).show();
    }
}



private boolean isMyServiceRunning(Class<?> serviceClass) {
    ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    for (RunningServiceInfo service : manager
            .getRunningServices(Integer.MAX_VALUE)) {
        Log.e("ALL SERVICE", service.service.getClassName().toString());
        if (serviceClass.getName().equals(service.service.getClassName())) {

            return true;
        }
    }
    return false;
}

But in Activity Manager Running Service Info, I am not getting the class of intent service that I am running, so this always stands false.

I have used Broadcast for API calls response.

I have even checked this code.

if(startService(someIntent) != null) { 
 Toast.makeText(getBaseContext(), "Service is already running",     Toast.LENGTH_SHORT).show();
} else {
 Toast.makeText(getBaseContext(), "There is no service running, starting     service..", Toast.LENGTH_SHORT).show();
} 

But in this code, on checking the service it also starts the service again, so service is called twice.

Please help me out with this.

Answer

AADProgramming picture AADProgramming · Feb 4, 2015

IntentService needs to implement onHandleIntent() This method is invoked on the worker thread with a request to process Intent request and IntentService will be "alive" as long as it is processing a intent request (am not considering low memory and other corener cases here, but just thinking in terms of logic),

And When all requests have been handled, the IntentService stops itself, (and hence you should not call stopSelf() explicitly)

With this theory in place, You may try below logic:
Declare a class variable inside your IntentService class.
public static boolean isIntentServiceRunning = false;

@Override    
     protected void onHandleIntent(Intent workIntent) { 
        if(!isIntentServiceRunning) {
         isIntentServiceRunning = true;
       }
      //Your other code here for processing request
 }

And in onDestroy() of IntentService class if you may choose, set isIntentServiceRunning = false;

And use isIntentServiceRunning to check if IntentService is Running!