Java tutorial
//package com.java2s; import android.app.Activity; import android.app.ActivityManager; import android.content.Context; import android.content.Intent; import android.util.Log; public class Main { /** * Function to stop a service. * * @param cls The service's class. */ public static void stopService(Class<?> cls, Activity activity) { if (serviceIsRunning(cls, activity)) { activity.stopService(new Intent(activity, cls)); Log.d("Utils Service", "Service Stopped"); } } /** * Function to check if a Service is running. * * @param serviceClass The service's class. * @return true if running, else false. */ private static boolean serviceIsRunning(Class<?> serviceClass, Activity activity) { ActivityManager manager = (ActivityManager) activity.getSystemService(Context.ACTIVITY_SERVICE); for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) { if (serviceClass.getName().equals(service.service.getClassName())) { return true; } } return false; } }