PhoneWindow: 在Activity#Attach中创建,如果Activity#onCreate调用setContentView,会调用PhoneWindow的setContentView中设置layout.xml则会生成DecorView和DecorView的子view ContentParent(DecorView添加R.layout.screen_simple.xml,ContentParent是其中的com.android.internal.R.id.content),然后将setContentView中的view添加到ContentParent。
DecorView:view的根布局,包含了ContentParent,ContentParent添加我们自己的view
ViewRootImpl:持有DecorView,发起WMS addView(DecorView),它有W作为WMS通信binder,InputChannel接收底层InputEvent然后通过DecorView分发,可以通过requestLayout刷新界面。其实一切都因为它持有DecorView,就可以通过DecorView就可以遍历到所有的view。
WindowManagerGlobal:app中所有的DecorView和ViewRootImpl都会保存在这里,算作是app的window综合管理者,addView removeView updateLayout都是从这里调用WMS的代理。
关键步骤:
ATS startActivityAsUser
ActivityStarter 做一些检查的问题
Task resumeTopActivityInnerLocked 这里区分有ProcessRecord或者没有
ActivityTaskSupervisor startSpecificActivity
ATS startProcess 创建进程 ProcessList startProcessLocked
Zygote forkAndSpecialize handleChildProc(走app进程)
ZygoteInit RuntimeInit.commonInit() RuntimeInit.applicationInit() RuntimeInit 这里注册Thread.UncaughtExceptionHandler打印崩溃日志堆栈,时区,http.agent
调用ActivityThread.main方法
ActivityThread
main:
AndroidOs.install();系统调用拦截,最终调用的是libcore.io.Linux.java,调用native方法完成系统调用
Environment.initForCurrentUser();当前app的一些环境配置,文件夹什么的
TrustedCertificateStore.setDefaultUserDirectory("/data/misc/user0");确保TrustedCertificateStore查找CA证书的正确位置,/data/misc/user0,可自定义
Looper.prepareMainLooper();创建主线程looper
ActivityThread thread = new ActivityThread();thread.attach(false, startSeq);参数里面的startSeq就是之前ATS调用startProcessLocked生成的,创建ActivityThread并attach
Looper.loop();启动死循环接收主线程消息
attach:
参数isSystem区分是否为system进程
mConfigurationController = new ConfigurationController(this);初始化ConfigurationController
RuntimeInit.setApplicationObject(mAppThread.asBinder()); //mAppThread就是ApplicationThread对象,用于和AMS交互的binder对象,设置到RuntimeInit也是为了AMS handleApplicationCrash之类的处理,通过ApplicationThread找到对应的process
mgr.attachApplication(mAppThread, startSeq); AMS attachApplication, 不是oneWay调用,会阻塞住
AMS
attachApplicationLocked:
根据startSeq找到ProcessRecord,ProcessList.java中startProcessLocked mProcStartSeqCounter自增,并且设置到ProcessRecord中,这个ProcessRecord保存在AMS中。
mProcessList.handleProcessStartedLocked:打印start Proc pid这段log,表示app的进程已经起来了。
mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app);移除启动超时机制
thread.bindApplication 调用ApplicationThread的bindApplication方法,回到app进程继续Activity启动周期
RootWindowContainer.attachApplication 阻塞住,等待thread.bindApplication执行完
ActivityThread
bindApplication -> handleBindApplication:
VMRuntime.registerSensitiveThread(); //Registers the current thread as the jit sensitive thread. Should be called just once.
mConfigurationController.setConfiguration(data.config);//设置config
Process.setArgV0(data.processName);//设置processName
VMRuntime.setProcessPackageName(data.appInfo.packageName);//设置processName
//设置VMRuntime data路径
//bitmap设置默认160
//update dpi
//设置12_24小时制
//网络安全相关配置 TLS
//VMRuntime清除堆
app = mActivityThread.mInstrumentation.newApplication(cl, appClass, appContext);appContext.setOuterContext(app);//创建Application
mInstrumentation.callApplicationOnCreate(app);//Application onCreate
//加载font资源
AMS
RootWindowContainer.java
attachApplication:
startActivityForAttachedApplicationIfNeeded
ActivityTaskSupervisor.java
startSpecificActivity
realStartActivityLocked mService.getLifecycleManager().scheduleTransaction(ResumeActivityItem.obtain(isTransitionForward));
ActivityThread Transaction生命周期这里使用了策略模式和状态机,针对每个生命周期都有对应的封装类处理逻辑和对应的状态切换
ActivityThread
handleLaunchActivity:
HardwareRenderer.preload();//硬件加速线程初始化
WindowManagerGlobal.initialize();//WindowManagerGlobal初始化,这里保存该app的所有add的View和root,也就是所有的Window的DecorView
final Activity a = performLaunchActivity(r, customIntent);
performLaunchActivity:
创建Activity context,Activity Context mContextType为CONTEXT_TYPE_ACTIVITY,application context为CONTEXT_TYPE_NON_UI,system的为CONTEXT_TYPE_SYSTEM_OR_SYSTEM_UI
//ResourcesLoader、ResourcesProvider可以实现外部加载资源的方案,android11
activity.attach
Activity
attach:
mWindow = new PhoneWindow(this, window, activityConfigCallback);//穿件PhoneWindow
//注册callback到PhoneWindow监听一些状态和回调
ActivityThread
handleResumeActivity:
View decor = r.window.getDecorView(); installDecor->generateDecor->new DecorView如果调用了setContentView,就会提前初始化
decor.setVisibility(View.INVISIBLE);先设置为不可见
WindowManagerGlobal.addView
WindowManagerGlobal
addView 创建root = new ViewRootImpl(view.getContext(), display);调用ViewRootImpl#setView
ViewRootImpl
setView:
设置view的mAttachInfo,持有ViewRootImpl
requestLayout;//刷新layout绘制流程
//初始化InputChannel,传递给WMS进行填充,InputChannel pair
mWindowSession.addToDisplayAsUser;//添加到WMS,传递信使W,与WMS交互信息
InputStage责任链初始化,分发InputEvent,Touch KeyEvnet的View分发是ViewPostImeStage。
WMS
addWindow 创建InputChanel pair,将window加入管理
Activity
makeVisible() mDecor.setVisibility(View.VISIBLE);
详细步骤
AMS startActivityAsUser
ATS startActivityAsUser
ActivityStarter startActivityAsUser
executeRequest (检查intent和permission)
boolean abort = !mSupervisor.checkStartAnyActivityPermission();
abort |= !mService.mIntentFirewall.checkStartActivity();
abort |= !mService.getPermissionPolicyInternal().checkStartActivity();
startActivityUnchecked
startActivityInner 在startActivityInner()的时候,基本上已经检查完毕了,会先去进行状态的初始化,也就是从AcitivityRecord当中将对应的参数存储到本地使用等。
Task.java
startActivityLocked 在这里开始,启动Activity的任务就交给了Task处理,不再交给ActivityStack进行处理了
ensureActivitiesVisible
forAllLeafTasks(task -> task.mEnsureActivitiesVisibleHelper.process) 在Android12上,采用的大量的Consumer的方式进行事件的处理
EnsureActivitiesVisibleHelper.java
process
setActivityVisibilityState
ActivityRecord
makeActiveIfNeeded
Task
resumeTopActivityUncheckedLocked 通过ActivityRecord getRootTask来进行后续处理
resumeTopActivityInnerLocked 这里会根据是否有attachProcess调用不同的逻辑,如果是的话,直接 ClientTransaction走起,如果不是的话,就往下走
ActivityTaskSupervisor.java
startSpecificActivity
ATS
startProcess 创建进程
ProcessList startProcessLocked
Zygote forkAndSpecialize handleChildProc(走app进程)
ZygoteInit RuntimeInit.commonInit() RuntimeInit.applicationInit()
RuntimeInit 这里注册Thread.UncaughtExceptionHandler打印崩溃日志堆栈,时区,http.agent,VMRuntime.getRuntime().setTargetSdkVersion(targetSdkVersion);VMRuntime.getRuntime().setDisabledCompatChanges(disabledCompatChanges);
调用ActivityThread.main方法
ActivityThread
main:
AndroidOs.install();系统调用拦截,最终调用的是libcore.io.Linux.java,调用native方法完成系统调用
Environment.initForCurrentUser();当前app的一些环境配置,文件夹什么的
TrustedCertificateStore.setDefaultUserDirectory("/data/misc/user0");确保TrustedCertificateStore查找CA证书的正确位置,/data/misc/user0,可自定义
Looper.prepareMainLooper();创建主线程looper
ActivityThread thread = new ActivityThread();thread.attach(false, startSeq);参数里面的startSeq,创建ActivityThread并attach
Looper.loop();启动死循环接收主线程消息
attach:
参数isSystem区分是否为system进程
sCurrentActivityThread = this;mConfigurationController = new ConfigurationController(this);初始化ConfigurationController
RuntimeInit.setApplicationObject(mAppThread.asBinder()); //mAppThread就是ApplicationThread对象,用于和AMS交互的binder对象,设置到RuntimeInit也是为了AMS handleApplicationCrash之类的处理,通过ApplicationThread找到对应的process
mgr.attachApplication(mAppThread, startSeq); AMS attachApplication, 不是oneWay调用,会阻塞住
AMS
attachApplicationLocked:
根据startSeq找到ProcessRecord,ProcessList.java中mProcStartSeqCounter()自增得到,并且设置到ProcessRecord中,这个ProcessRecord保存在AMS中。
mProcessList.handleProcessStartedLocked:打印start Proc pid这段log,表示app的进程已经起来了。
mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app);启动超时机制
thread.bindApplication 调用ApplicationThread的bindApplication方法,回到app进程继续Activity启动周期
updateLruProcessLocked(app, false, null); ProcessList更新Lru
mPersistentStartingProcesses.remove(app);
mProcessesOnHold.remove(app);
mAtmInternal.attachApplication(app.getWindowProcessController());
RootWindowContainer.attachApplication:
ActivityThread
bindApplication:
初始化app,BIND_APPLICATION
handleBindApplication:
VMRuntime.registerSensitiveThread(); Registers the current thread as the jit sensitive thread. Should be called just once.
mConfigurationController.setConfiguration(data.config);
mConfigurationController.setCompatConfiguration(data.config);
Process.setArgV0(data.processName);
VMRuntime.setProcessPackageName(data.appInfo.packageName);
VMRuntime.setProcessDataDirectory(data.appInfo.dataDir);
Bitmap.setDefaultDensity(DisplayMetrics.DENSITY_DEFAULT);设置默认160
mConfigurationController.updateDefaultDensity(data.config.densityDpi);update dpi
final String use24HourSetting = mCoreSettings.getString(Settings.System.TIME_12_24); DateFormat.set24HourTimePref(is24Hr);设置12_24小时制
NetworkSecurityConfigProvider.install(appContext); Install the Network Security Config Provider. This must happen before the application, code is loaded to prevent issues with instances of TLS objects being created before the provider is installed.
dalvik.system.VMRuntime.getRuntime().clearGrowthLimit();清除堆
app = data.info.makeApplication(data.restrictedBackupMode, null);
app = mActivityThread.mInstrumentation.newApplication(cl, appClass, appContext);appContext.setOuterContext(app);
mInstrumentation.onCreate(data.instrumentationArgs);
mInstrumentation.callApplicationOnCreate(app);Application onCreate
data.info.getResources().preloadFonts(preloadedFontsResource);
AMS
RootWindowContainer.java
attachApplication:
startActivityForAttachedApplicationIfNeeded
ActivityTaskSupervisor.java
startSpecificActivity
realStartActivityLocked mService.getLifecycleManager().scheduleTransaction(ResumeActivityItem.obtain(isTransitionForward));
ActivityThread Transaction生命周期这里使用了策略模式和状态机,针对每个生命周期都有对应的封装类处理逻辑和对应的状态切换
ActivityThread
handleLaunchActivity:
HardwareRenderer.preload();
WindowManagerGlobal.initialize();
final Activity a = performLaunchActivity(r, customIntent);
performLaunchActivity:
创建Activity context,Activity Context mContextType为CONTEXT_TYPE_ACTIVIT,application context为CONTEXT_TYPE_NON_UI,system的为CONTEXT_TYPE_SYSTEM_OR_SYSTEM_UI
appContext.getResources().addLoaders(app.getResources().getLoaders().toArray(new ResourcesLoader[0]));ResourcesLoader、ResourcesProvider可以实现外部加载资源的方案,android11
activity attach
Activity.java
attach:
mWindow = new PhoneWindow(this, window, activityConfigCallback);
mWindow.setWindowControllerCallback(mWindowControllerCallback);
mWindow.setCallback(this);
mWindow.setOnWindowDismissedCallback(this);
ActivityThread.java
handleResumeActivity:
View decor = r.window.getDecorView(); installDecor->generateDecor->new DecorView如果调用了setContentView,就会提前初始化
decor.setVisibility(View.INVISIBLE);先设置为不可见
WindowManagerGlobal
addView 创建root = new ViewRootImpl(view.getContext(), display);并且ViewRootImpl#setView->设置view的mAttachInfo为ViewRootImpl对象的mAttachInfo->requestLayout()刷新layout绘制流程
ViewRootImpl
setView mWindowSession.addToDisplayAsUser,添加class W(Binder通信)到WMS,初始化InputChannel,创建InputChannel pair,为接收Input事件提供接收通道;InputStage责任链初始化,分发InputEvent,View分发是ViewPostImeStage。
Activity
makeVisible() mDecor.setVisibility(View.VISIBLE);
Android12 增加allapp 安卓12教程
转载本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
android12没有monkey 安卓monkey
先看简介,然后给出一个使用的小实例。 简介 Monkey是一个命令行工具,可以运行在模拟器里或实际设备中。它向系统发送伪随机的用户事件流,实现对正在开发的应用程序进行压力测试。Monkey包括许多选项,它们大致分为四大类: · 基本配置选项,如设置尝试的事件数量。 · 运行约束选项,如设置只对单独的一个包进行测试。 · 事件类型和频率。 · 调
android12没有monkey Android UP Mobile Windows Mobile