Android检测某个服务是否还活着代码
工具类:校验某个服务是否还活着
import java.util.List; import android.app.ActivityManager; import android.app.ActivityManager.RunningServiceInfo; import android.content.Context; public class ServiceUtils { /** * 校验某个服务是否还活着 * serviceName :传进来的服务的名称 */ public static boolean isServiceRunning(Context context,String serviceName){ ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); List<RunningServiceInfo> infos = am.getRunningServices(100); for(RunningServiceInfo info : infos){ String name = info.service.getClassName(); if(name.equals(serviceName)){ return true; } } return false; } }