通常來講Application的onCreate方法只會執行一次,android
若是應用中採用多進程方式,oncreate方法會執行屢次,根據不一樣的進程名字進行不一樣的初始化:web
String processName = getProcessName(this, android.os.Process.myPid()); if (processName != null) { boolean defaultProcess = processName.equals(Constants.REAL_PACKAGE_NAME); if (defaultProcess) { initAppForMainProcess(); } else if (processName.contains(":webbrowser")) { } else if (processName.contains(":wallet")) { } }
/** * @return null may be returned if the specified process not found */ public static String getProcessName(Context cxt, int pid) { ActivityManager am = (ActivityManager) cxt.getSystemService(Context.ACTIVITY_SERVICE); List<RunningAppProcessInfo> runningApps = am.getRunningAppProcesses(); if (runningApps == null) { return null; } for (RunningAppProcessInfo procInfo : runningApps) { if (procInfo.pid == pid) { return procInfo.processName; } } return null; }
新增一種獲取運行進程名稱的方案:this
public static String getProcessName() {spa
try {code
File file = new File("/proc/" + android.os.Process.myPid() + "/" + "cmdline");進程
BufferedReader mBufferedReader = new BufferedReader(new FileReader(file));ci
String processName = mBufferedReader.readLine().trim();get
mBufferedReader.close();cmd
return processName;it
} catch (Exception e) {
e.printStackTrace();
return null;
}
}