判断Service是否已经启动

 /** 查看服务是否开启*/
    public static Boolean isServiceRunning(Context context, String serviceName) {
        //获取服务方法  参数 必须用大写的Context!!!
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningServiceInfo> infos = am.getRunningServices(100);
        for (RunningServiceInfo info : infos) {
            String className = info.service.getClassName();
            if(serviceName.equals(className))
                return true;
        }
        return false;
    }
posted on 2016-08-08 15:52  白衣雨果  阅读(2181)  评论(1编辑  收藏  举报