Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: How to determine if IntentService is running?

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.

like image 943
Ruchit Shah Avatar asked Feb 03 '15 12:02

Ruchit Shah


2 Answers

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!

like image 77
AADProgramming Avatar answered Sep 30 '22 04:09

AADProgramming


In your code you are trying to get the status of service from Activity. The correct way is that status should be given by the service.

There are several possibilities for an activity to communicate with a service and vice versa. As you are using Broadcast receiver so you can broadcast a message in onHandleIntent() method when the service started.

and then when your task completes or in case of any error, again you can call the broadcast receiver for service finished event.

here is a link for a nice tutorial.

like image 45
Sandeep Solanki Avatar answered Sep 30 '22 05:09

Sandeep Solanki