[Android Pro] Android中全局Application的onCreate多次调用问题

 一般来说Application的onCreate方法只会执行一次,

如果应用中采用多进程方式,oncreate方法会执行多次,根据不同的进程名字进行不同的初始化,

就是在application中多添加几个if else

        String processName = OsUtils.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")) {
                initAppForWebBrowseProcess();
            } 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;
    }

 

posted @ 2015-01-05 14:08  demoblog  阅读(12053)  评论(2编辑  收藏  举报