How to check if IntentService is running

I would like to know if the operation started successfully IntentService.

How can one bind IntentServicethrough bindService()in order for it to work, perhaps the approach was to check whether the call is making a startService(intent)call onStartCommand(..)or onHandleIntent(..)in the service object.

But how can I check that in an Activity?

+4
source share
4 answers

I would like to know if the Activity Activity IntentService has started successfully.

If you get an exception neither in action nor in the service when called startService(), then it was launched IntentService.

How can I bind an IntentService with bindService () to make it work

Why?

+2
source

, , , . Sercive - DroidUptimeService.

private boolean isServiceRunning() {
    ActivityManager activityManager = (ActivityManager)getSystemService(ACTIVITY_SERVICE);
    List<ActivityManager.RunningServiceInfo> serviceList = activityManager.getRunningServices(Integer.MAX_VALUE);

    if (serviceList.size() <= 0) {
        return false;
    }
    for (int i = 0; i < serviceList.size(); i++) {
        RunningServiceInfo serviceInfo = serviceList.get(i);
        ComponentName serviceName = serviceInfo.service;
        if (serviceName.getClassName().equals(DroidUptimeService.class.getName())) {
            return true;
        }
    }

    return false;
}
+7

You can add a flag when building PendingIntent, if the return value was null, your service is not running. The specified flag PendingIntent.FLAG_NO_CREATE.

Intent intent = new Intent(yourContext,YourService.class);
PendingIntent pendingIntent =   PendingIntent.getService(yourContext,0,intent,PendingIntent.FLAG_NO_CREATE);

if (pendingIntent == null){
    return "service is not created yet";
} else {
    return "service is already running!";
}
+5
source

Here is the method I use to check if my service is working:

  public static boolean isMyServiceRunning(Class<?> serviceClass, Context context) {
        ActivityManager manager = (ActivityManager) context.getSystemService(ACTIVITY_SERVICE);
        for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
            if (serviceClass.getName().equals(service.service.getClassName())) {
                return service.started;
            }
        }
        return false;
    }
0
source

All Articles