package com.secspace.lib.common.utils;

import android.app.ActivityManager;
import android.content.Context;
import android.util.Log;

import java.util.List;

/**
 * Created by cyw_m on 2018/4/9.
 */

public class CompnentHelper {
    private static final String TAG = "CompnentHelper";

    /**
     * 自己的某个service是否正在运行
     *
     * @param mContext
     * @param serviceName
     * @return
     */
    public static boolean isServiceWorking(Context mContext, String serviceName) {
        boolean working = false;
        ActivityManager myAM = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningServiceInfo> myList = myAM.getRunningServices(Integer.MAX_VALUE);
        if (myList.size() <= 0) {
            return false;
        }
        for (int i = 0; i < myList.size(); i++) {
            String name = myList.get(i).service.getClassName().toString();
            Log.d(TAG, "isServiceWork: " + name);
            if (name.equals(serviceName)) {
                working = true;
                break;
            }
        }
        return working;
    }
}
