android

android获取processId

2018-10-20  本文已影响0人  WLHere
android.os.Process.myPid()
public static int getPidByProcessName(Context context, String packageName) {
        if (StringUtils.isBlank(packageName)) {
            return -1;
        }
        ActivityManager mActivityManager = (ActivityManager) context
                .getSystemService(Context.ACTIVITY_SERVICE);
        // 通过调用ActivityManager的getRunningAppProcesses()方法获得系统里所有正在运行的进程
        List<ActivityManager.RunningAppProcessInfo> appProcessList = mActivityManager
                .getRunningAppProcesses();
        if (appProcessList != null) {
            for (ActivityManager.RunningAppProcessInfo appProcess : appProcessList) {
                if (packageName.equals(appProcess.processName)) {
                    return appProcess.pid;
                }
            }
        } 
        return -1;
    }
上一篇下一篇

猜你喜欢

热点阅读