Android四大组件之Activity(一)—— 启动过程综述

Android四大组件之Activity(一)—— 启动过程综述,第1张

概述1、使用的是Android7.1.2的源代码:https://pan.baidu.com/s/1XcVD68cC_2wKtm8jJkdNQAprzv2、感谢IT先森的系列博客:Android应用进程创建流程大揭秘Android四大组件之bindService源码实现详解Android四大组件之Activity启动流程源码实现详解概要Activity启动流程(一)

1、使用的是AndroID 7.1.2的源代码:

https://pan.baIDu.com/s/1XcVD68cC_2wKtm8jJkdNQA
przv

2、感谢IT先森的系列博客:

Android应用进程创建流程大揭秘
Android四大组件之bindService源码实现详解
Android四大组件之Activity启动流程源码实现详解概要
Activity启动流程(一)发起端进程请求启动目标Activity
Activity启动流程(二)system_server进程处理启动Activity请求
Activity启动流程(三)-Activity Task调度算法复盘分析
Activity启动流程(四)-Pause前台显示Activity,Resume目标Activity
Activity启动流程(五)请求并创建目标Activity进程
Activity启动流程(六)注册目标Activity进程到system_server进程以及创建目标Activity进程Application
Activity启动流程(七)初始化目标Activity并执行相关生命周期流程

伪代码:
发起端进程发起启动Activity请求	Activity.startActivity(...)	  Activity.startActivityForResult(...) //mTocken是Activity中的	    mInstrumentation.execStartActivity(...)		  AMP.startActivity(...)//通过AMS代理端向AMS发起启动Activity的Binder IPC请求		  mInstrumentation.checkStartActivityResult(...)//检测启动是否成功		mMainThread.sendActivityResult(...)system_server进程端AMS解析Intent	  	AMS.startActivity(...)	 ActivityStarter.startActivityMayWait(...)	   ResolveInfo rInfo = ASS.resolveIntent(...)//收集Intent所指向的Activity信息, 当存在多个可供选择的Activity,则直接向用户d出resolveActivity	     IPackageManager.Stub.resolveIntent(...)//通过PKMS实体查询		   PKMS.resolveIntent(...)			 PKMS.queryIntentActivitIEsInternal(...)			 PKMS.chooseBestActivity(...)	   ActivityInfo aInfo = ASS.resolveActivity(...) //根据获取的rInfo信息重新组装intent和设置启动的参数信息	   //创建目的端Activity的ActivityRecord	   ActivityStarter.startActivityLocked(...)		  ActivityRecord r = new ActivityRecord(callerApp,intent,aInfo,mSupervisor,...)			appToken = new Token(this, _intent);			  设置state为INITIAliZING		  ActivityStarter.startActivityUnchecked(...)            //包括两方面内容:           //1、AMS对于Activity比较复杂的部分即Task任务栈和Stack的处理,其中会把目标Activity放置到栈顶端           //2、resumed相关 *** 作            // 目标Activity所属Stack栈存在Resume状态的Activity时会执行其onPause方法,否则执行的就是其它Stack栈中的了           // 错误说法:启动一个Activity的时候最先被执行的是栈顶的Activity的onPause方法           		ActivityStackSupervisor.resumeFocusedStacktopActivityLocked    			ActivityStack.resumetopActivityUncheckedLocked    				ActivityStack.resumetopActivityInnerLocked    					ActivityStack.startPausingLocked    						ApplicationThreadProxy.schedulePauseActivity          	               发起端进程处理schedulePauseActivity   ActivityThread.ApplicationThread.schedulePauseActivity        ActivityThread.H.handleMessage(case PAUSE_ACTIVITY)             ActivityThread.handlePauseActivity //1.指定前台Activity的onPause();2.通过AMP回调activityPaused()通知AMS             	ActivityThread.performPauseActivity              		ActivityThread.performPauseActivityIfNeeded              			Instrumentation.callActivityOnPause              				Activity.performPause              					Activity.onPause()                ActivityManagerNative.getDefault().activityPaused(token);system_server进程端    AMS.activityPaused    	ActivityStack.activityPausedLocked    		ActivityStack.completePauseLocked    			ActivityStackSupervisor.resumeFocusedStacktopActivityLocked  //又调用了resumeFocusedStacktopActivityLocked    				ActivityStack.resumetopActivityUncheckedLocked    					ActivityStack.resumetopActivityInnerLocked    						ActivityStackSupervisor.startSpecificActivityLocked    							AMS.startProcessLocked    								AMS.startProcessLocked    									AMS.newProcessRecordLocked    										addProcessnameLocked(r);//将ProcessRecord信息在AMS中存储起来    									AMS.startProcessLocked                                     		//final String entryPoint = "androID.app.ActivityThread";                                   	 		Process.start(entryPoint, ··· ···)                                       		Process.startViaZygote()	                                       	 		Process.zygot                                            eSendArgsAndGetResult(openZygoteSocketIfNeeded(                                       abi), argsForZygote);//======Zygote进程======    ZygoteInit.main    //1    ZygoteInit.registerZygoteSocket  //创建zygote通信服务端,LocalServerSocket实例对象sServerSocket    //2    ZygoteInit.runSelectLoop  //进入无限死循环,等待客户端的请求;会抛出MethodAndArgsCaller异常    	Os.poll    	ZygoteInit.acceptCommandPeer //创建ZygoteConnection对象,即创建Socket通道ZygoteConnection    		ZygoteConnection.ZygoteConnection    	ZygoteConnection.runOnce    		Zygote.forkAndSpecialize                    ZygoteHooks.preFork                        Daemons.stop                        ZygoteHooks.nativePreFork                            dalvik_system_ZygoteHooks.ZygoteHooks_nativePreFork                                Runtime::PreZygoteFork                                    heap_->PreZygotefork()                    Zygote.nativeForkAndSpecialize                        com_androID_internal_os_Zygote.ForkAndSpecializeCommon                            fork()                            Zygote.callPostForkChildHooks                                ZygoteHooks.postForkChild                                    dalvik_system_ZygoteHooks.nativePostForkChild                                        Runtime::DIDForkFromZygote                    ZygoteHooks.postForkCommon                        Daemons.start                                            	//======子进程======                                                     ZygoteConnection.handleChildProc                    RuntimeInit.zygoteInit                        RuntimeInit.commonInit                        RuntimeInit.nativeZygoteInit                        RuntimeInit.applicationInit                            RuntimeInit.invokeStaticMain  //抛出异常:MethodAndArgsCaller     MethodAndArgsCaller.run() //runSelectLoop中抛出异常    	ActivityThread.main                                                                                        //3		    ZygoteInit.closeServerSocket //关闭通信socket通信通道 //新建的子进程ZygoteConnection.handleChildProc    Activity.main()  //反射调用    	Looper.prepareMainLooper();    	ActivityThread thread = new ActivityThread();			final ApplicationThread mAppThread = new ApplicationThread();			final H mH = new H();		thread.attach(false);			RuntimeInit.setApplicationObject(mAppThread.asBinder())			AMN.getDefault().attachApplication(mAppThread) //mAppThread为匿名Binder		Looper.loop();//system_server进程AMS.attachApplication(ApplicationThread thread)	thread.bindApplication  //向目标Activity进程发起跨进程Binder调用,将进程名、ApplicationInfo等相关信息传递给应用进程	ActivityStackSupervisor.attachApplicationLocked(ProcessRecord app)  //检查有没有待启动的Activity		app.thread.scheduleLaunchActivity  		··· ···//新建的子进程ActivityThread.ApplicationThread.scheduleLaunchActivity	sendMessage(H.LAUNCH_ACTIVITY, r) 	ActivityThread.H.handleMessage		ActivityThread.handleLaunchActivity			Activity a = performlaunchActivity				mInstrumentation.callActivityOnCreate					Activity.performCreate						Activity.onCreate()				Activity.performStart					mInstrumentation.callActivityOnStart						Activity.onStart				ActivityThread.handleResumeActivity				performResumeActivity					Activity.performResume						mInstrumentation.callActivityOnResume							Activity.onResume	

总结

以上是内存溢出为你收集整理的Android四大组件之Activity(一)—— 启动过程综述全部内容,希望文章能够帮你解决Android四大组件之Activity(一)—— 启动过程综述所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1046518.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存