PHP
·
发表于 5年以前
·
阅读量:8312
获取当前的进程名,一般用来在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;
}