创建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
相关推荐
无极程序员35 分钟前
PHP常量
android·ide·android studio
萌面小侠Plus2 小时前
Android笔记(三十三):封装设备性能级别判断工具——低端机还是高端机
android·性能优化·kotlin·工具类·低端机
慢慢成长的码农2 小时前
Android Profiler 内存分析
android
大风起兮云飞扬丶2 小时前
Android——多线程、线程通信、handler机制
android
L72562 小时前
Android的Handler
android
清风徐来辽2 小时前
Android HandlerThread 基础
android
HerayChen3 小时前
HbuildderX运行到手机或模拟器的Android App基座识别不到设备 mac
android·macos·智能手机
顾北川_野3 小时前
Android 手机设备的OEM-unlock解锁 和 adb push文件
android·java
hairenjing11233 小时前
在 Android 手机上从SD 卡恢复数据的 6 个有效应用程序
android·人工智能·windows·macos·智能手机
小黄人软件3 小时前
android浏览器源码 可输入地址或关键词搜索 android studio 2024 可开发可改地址
android·ide·android studio