/**
* 判断服务是否开启
*
* @param mContext
* @param className 这里是包名+类名 xxx.xxx.xxx.TestService
* @return
*/
public static boolean isServiceRunning(Context mContext,String className) {
boolean isRunning = false;
ActivityManager activityManager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningServiceInfo> serviceList = activityManager.getRunningServices(Integer.MAX_VALUE);
if (!(serviceList.size()>0)) {
return isRunning;
}
for (int i=0; i < serviceList.size(); i++) {
if (serviceList.get(i).service.getClassName().equals(className) == true) {
isRunning = true;
break;
}
}
return isRunning;
}
有人说为啥我这代码没用?宝宝告诉你,那是应为你 获取正在运行中的服务的最大值写的是固定的值,所以它只能获取到你设置的固定的值。比如20,那就只能获取到运行中的20个运行的服务。