/** * 判断服务是否正在运行 * * @param context 上下文 * @param serviceName 服务名称,包名加类名 * @return 服务正在运行则返回true,否则返回false */ public static boolean isServiceWorked(Context context, String serviceName) { ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); ArrayList<ActivityManager.RunningServiceInfo> runningServices = (ArrayList<ActivityManager.RunningServiceInfo>) activityManager.getRunningServices(Integer.MAX_VALUE); for (ActivityManager.RunningServiceInfo runningService : runningServices) { String name = runningService.service.getClassName().toString(); if (name.equals(serviceName)) { return true; } } return false; }