獲取當前的進程名

獲取當前的進程名,一般用來在App中判斷當前進程是否為主進程(相對于應用的服務進程或自定義的進程來說)。

private String getCurrentProcessName(Context context) {
    int pid = android.os.Process.myPid();
    ActivityManager activityManager = ;
    if (activityManager == null) {
        return null;
    }
    List<ActivityManager.RunningAppProcessInfo> appProcessList = activityManager.getRunningAppProcesses();
    if (appProcessList != null && appProcessList.length() > 0) {
        for (ActivityManager.RunningAppProcessInfo appProcess : appProcessList) {
            if (appProcess.pid == pid) {
                return appProcess.processName;
            }
        }

    }
    return null;
}

所屬標簽

無標簽

25选5玩法中奖