/**
* Judge a service is run or not.
* @param context Current application context.
* @param className The service class name.
* @return Return true if service is running, false or not.
*/
public static boolean isServiceRun(Context context, String className) {
boolean isRun = false;
final int maxNum = 100;
if (context == null || className == null) {
LogHelper.e(TAG, "isServiceRun mContext = " + context + " className = " + className);
return false;
}
try {
ActivityManager activityManager = (ActivityManager) context
.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningServiceInfo> serviceList = activityManager
.getRunningServices(maxNum);
int size = serviceList.size();
for (int i = 0; i < size; i++) {
if (serviceList.get(i).service.getClassName().equals(className)) {
isRun = true;
break;
}
}
} catch (Exception ex) {
LogHelper.e(TAG, "isServiceRun " + ex);
}
LogHelper.d(TAG, "isServiceRun service name = " + className + " is run " + isRun);
return isRun;
}