public static String getProcessName(Context cxt, int pid) {
ActivityManager am = (ActivityManager) cxt.getSystemService(Context.ACTIVITY_SERVICE);
List runningApps = am.getRunningAppProcesses();
if (runningApps == null) {
return null;
}
for (RunningAppProcessInfo procInfo : runningApps) {
if (procInfo.pid == pid) {
return procInfo.processName;
}
}
return null;
}
在 Application 的 onCreate 中获取进程名称并进行相应的判断,例如:
String processName = getProcessName(this, android . os . Process . myPid());
if (!TextUtils.isEmpty(processName) && processName.equals(this.getPackageName())) {//判断进程名,保证只有主进程运行
//主进程初始化逻辑
….
}重点内容
版权声明:本文为QQ396371485原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。