android项目判断service是否存在

思路是这样的获取所有运行服务的名字,然后和自己服务的名字比对,

public static boolean isServiceAlive(Context context, String serviceName) {
        boolean flag = false;
        ActivityManager am = (ActivityManager) context
                .getSystemService(Activity.ACTIVITY_SERVICE);
        //返回100个服务
        List<RunningServiceInfo> runningServices = am.getRunningServices(100);
        for (RunningServiceInfo runningServiceInfo : runningServices) {
            String allServiceName = runningServiceInfo.service.getClassName();
            if(allServiceName.equals(serviceName)){
                flag = true;
            }
        }
        return flag;
    }

 

posted @ 2015-12-03 16:05  aaddrrooiidd  阅读(801)  评论(0编辑  收藏  举报