版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Mr_theSun/article/details/88638508
/**
* 判断某个服务是否正在运行的方法
*
* @param serviceName 是包名+服务的类名
* @return true代表正在运行,false代表服务没有正在运行
*/
public static boolean isServiceWork(Context context,String serviceName) {
ActivityManager myAM = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
if (myAM != null) {
List<ActivityManager.RunningServiceInfo> serviceList = myAM.getRunningServices(Integer.MAX_VALUE);
if (serviceList != null && serviceList.size() > 0) {
for (ActivityManager.RunningServiceInfo info:serviceList) {
String inServiceName = info.service.getClassName();
if (serviceName.equals(inServiceName)){
return true;
}
}
}
}
return false;
}
就是上图的方法有几率触发