创建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
相关推荐
灿烂阳光g2 小时前
domain_auto_trans,source_domain,untrusted_app
android·linux
低调小一4 小时前
Android传统开发 vs Android Compose vs HarmonyOS ArkUI 对照表
android·华为·harmonyos
雨白4 小时前
Java 多线程指南:从基础用法到线程安全
android·java
00后程序员张5 小时前
详细解析苹果iOS应用上架到App Store的完整步骤与指南
android·ios·小程序·https·uni-app·iphone·webview
程序员江同学7 小时前
ovCompose + AI 开发跨三端的 Now in Kotlin App
android·kotlin·harmonyos
2501_915106327 小时前
Xcode 上传 ipa 全流程详解 App Store 上架流程、uni-app 生成 ipa 文件上传与审核指南
android·macos·ios·小程序·uni-app·iphone·xcode
消失的旧时光-19437 小时前
Kotlinx.serialization 使用讲解
android·数据结构·android jetpack
灿烂阳光g8 小时前
SELinux 策略文件编写
android·linux
.豆鲨包8 小时前
【Android】Viewpager2实现无限轮播图
android·java