创建activity中的context

performLaunchActivity的时候获取final int displayId = ActivityClient.getInstance().getDisplayId(r.token),调用createDisplayContext创建context

java 复制代码
@frameworks/base/core/java/android/app/ActivityThread.java
ActivityThread.performLaunchActivity()
    ContextImpl appContext = createBaseContextForActivity(r);
        final int displayId = ActivityClient.getInstance().getDisplayId(r.token);
        ContextImpl appContext = ContextImpl.createActivityContext(this, r.packageInfo, r.activityInfo, r.token, displayId, r.overrideConfig);
        for (int id : dm.getDisplayIds()) {
            isplay display = dm.getCompatibleDisplay(id, appContext.getResources());
            appContext = (ContextImpl) appContext.createDisplayContext(display);
@frameworks/base/core/java/android/app/ContextImpl.java
                ContextImpl context = new ContextImpl(null, mainThread, packageInfo, ContextParams.EMPTY, attributionTag, null, activityInfo.splitName, activityToken, null, 0, classLoader, null);
                context.mDisplay = resourcesManager.getAdjustedDisplay(displayId, context.getResources());  //记录Display
@frameworks/base/core/java/android/app/Activity.java
    activity.attach(appContext, this, getInstrumentation(), r.token, r.ident, app, r.intent, r.activityInfo, title, r.parent, config,r.referrer, r.voiceInteractor, window,);
        mWindow = new PhoneWindow(this, window, activityConfigCallback);
@frameworks/base/core/java/com/android/internal/policy/PhoneWindow.java
            mContext = context;  //PhoneWindow设置mContext为Activity里的context
        mWindow.setWindowManager((WindowManager)context.getSystemService(Context.WINDOW_SERVICE), mToken, (info.flags & ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0);
        //context是attach传入的值,使用context创建WindowManager
        if (wm == null) {  //如果wm为空则创建使用mContext创建WindowManager,mContext是PhoneWindow保存的值
            wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
        }
        mWindowManager = ((WindowManagerImpl)wm).createLocalWindowManager(this);
            return new WindowManagerImpl(mContext, parentWindow, mWindowContextToken); //创建WindowManagerImpl,mContext是wm的,wm有两种情况
                mContext = context;
                mParentWindow = parentWindow;



1. Activity 为例来分析应用窗口的添加过程
@frameworks/base/core/java/android/app/ActivityThread.java
handleResumeActivity
@frameworks/base/core/java/android/view/WindowManagerImpl.java
    View decor = r.window.getDecorView();
    wm.addView(decor, l);
@frameworks/base/core/java/android/app/ContextImpl.java
        getDisplayNoVerify
            return mDisplay;
        mGlobal.addView(view, params, mContext.getDisplayNoVerify(), mParentWindow, mContext.getUserId());    //WindowManagerImpl的mContext
相关推荐
Cao_Shixin攻城狮3 小时前
Flutter运行Android项目时显示java版本不兼容(Unsupported class file major version 65)的处理
android·java·flutter
呼啦啦呼啦啦啦啦啦啦6 小时前
利用pdfjs实现的pdf预览简单demo(包含翻页功能)
android·javascript·pdf
idjl7 小时前
Mysql测试题
android·adb
游戏开发爱好者810 小时前
iOS App 电池消耗管理与优化 提升用户体验的完整指南
android·ios·小程序·https·uni-app·iphone·webview
人生游戏牛马NPC1号10 小时前
学习 Flutter (四):玩安卓项目实战 - 中
android·学习·flutter
星辰也为你祝福h12 小时前
Android原生Dialog
android
梁同学与Android13 小时前
Android ---【CPU优化】需要优化的原因及优化的地方
android
Misha韩13 小时前
React Native 基础tabBar和自定义tabBar - bottom-tabs
android·react native
iHero13 小时前
【Nextcloud】在 Ubuntu 22.04.3 LTS 上的 Nextcloud Hub 10 (31.0.2) 后台任务cron 的优化
android·linux·ubuntu·nextcloud
yuanlaile17 小时前
Flutter Android打包学习指南
android·flutter·flutter打包·flutter android