编程语言
首页 > 编程语言> > Android开发源码解读四大组件源码解读简单梳理

Android开发源码解读四大组件源码解读简单梳理

作者:互联网

Android开发四大组件源码解读简单梳理

  1. Activity启动流程

    //主进程-main
    Activity.startActivity()
    mInstrumentation.execStartActivity()
    ActivityTaskManager.getService().startActivity()
    	- final IBinder b = ServiceManager.getService(Context.ACTIVITY_TASK_SERVICE);
        - return IActivityTaskManager.Stub.asInterface(b);
    //准备Stack有关的,新的binder进程-AMS
    ActivityTaskManagerService.startActivity()
    getActivityStartController().obtainStarter()...execute();//...组装Request(),接着执行
    ActivityStarter.executeRequest(Request request);//启动器执行Request,添加Activity到ActivityStack
    	- ActivityRecord r = new ActivityRecord()
    	- startActivityUnchecked()
    	- startActivityInner() 
    	- addOrReparentStartingActivity();//parent.addChild(mStartActivity)添加Activity到ActivityStack
    	- mTargetStack.getStack().moveToFront("reuseOrNewTask", targetTask);
    ActivityStack.startActivityLocked()	//执行stack逻辑,比如判断是否显示前一个Activity窗口过程等等
    ActivityRecord.showStartingWindow()	//执行当前Activity的show window逻辑
    
    //可能需要新进程,比如第一次启动进程还不存在
    ActivityStarter.startActivityInner()
    	RootWindowContainer.resumeFocusedStacksTopActivities()
    		//如果新进程,则增加下面三个逻辑异步创建进程
    		- ActivityStack.resumeTopActivityUncheckedLocked()
    		- ActivityStack.resumeTopActivityInnerLocked
    		- mAtmService.startProcessAsync(); //mAtmService是ActivityTaskManagerService
    		- ActivityManagerInternal::startProcess	//创建进程
    		- ActivityManagerService::LocalService::startProcess()	//最好还是回到AMS内部类
    		- ProcessList.startProcessLocked()
    		- ProcessList.startProcess()
    		- Process.start(entryPoint,...) //entryPoint就是“android.app.ActivityThread ”
    	mSupervisor.mRecentTasks.add(mStartActivity.getTask());//最后添加到最近任务列表
    
    //准备窗口有关的,这里东西比较多,也比较底层,涉及到Display、Surface、WmService等等
    ActivityTaskManagerService.continueWindowLayout()
    DisplayContent、DisplayManagerService
    mWmService.mDisplayManagerInternal.performTraversal(mDisplayTransaction);
    
    //Activity生命周期有关的,底层Binder-AMS发送各种和生命周期有关的Transaction与主线程ActivityThread交互
    ClientTransactionHandler.scheduleTransaction()
    ActivityThread(extends ClientTransactionHandler).sendMessage()
    	- ActivityThread.mH.sendMessage(msg);
    	- mTransactionExecutor.execute(transaction);//LaunchActivityItem
    LaunchActivityItem.execute()
    	- client.handleLaunchActivity(r, pendingActions, null);
    	- ActivityThread.performLaunchActivity()
    
  2. Service启动流程

    //main
    Context.startService()
    ContextImpl.startServiceCommon()
    //AMS
    ActivityManager.getService().startService()
    ActivityManagerService.startService()
    ActiveServices.startServiceLocked()	//ActiveServices管理Service启动关闭,前后台,启动是否超时等等
    	- startServiceInnerLocked
    	- bringUpServiceLocked
    	- realStartServiceLocked
    	- app.thread.scheduleCreateService()//ApplicationThread
    //main
    ApplicationThread.scheduleCreateService()
    	- sendMessage(H.CREATE_SERVICE, s);
    	- ActivityThread.handleCreateService()
    
  3. Broadcast接收与发送广播流程

    //main
    Context.registerReceiver()
    ContextImpl.registerReceiverInternal()
    LoadedApk.getReceiverDispatcher()	//获取Dispatcher,保存Receiver
    	- ReceiverDispatcher.performReceive()
    //AMS
    ActivityManager.getService().registerReceiverWithFeature()
    	- rl = new ReceiverList(this, callerApp, callingPid, callingUid, userId, receiver);
    	- mRegisteredReceivers.put(receiver.asBinder(), rl);	//AMS的mRegisteredReceivers保存封装的接收者列表
    	- BroadcastFilter bf = new BroadcastFilter(filter, rl, callerPackage, ...);
    	- rl.add(bf);
    
    //main
    Context.sendBroadcast
    ContextImpl.sendBroadcast
    //AMS
    ActivityManager.getService().broadcastIntentWithFeature()
    ActivityManagerService.broadcastIntentWithFeature()
    ActivityManagerService.broadcastIntentLocked()
    	- BroadcastRecord r = new BroadcastRecord()
    	- queue.enqueueOrderedBroadcastLocked(r);
    	- ActivityManagerService持有三类广播,前台广播,后台广播,离线广播
    		BroadcastQueue mFgBroadcastQueue;	//是否为前台广播通过Intent的flag指定
        	BroadcastQueue mBgBroadcastQueue;	//默认是后台广播
        	BroadcastQueue mOffloadBroadcastQueue;	//比如BOOT_COMPLETED广播
        	mStickyBroadcasts //粘性广播使用SparseArray保存
    BroadcastQueue.scheduleBroadcastsLocked()	//Handler转发一次
    BroadcastQueue.processNextBroadcast()
    BroadcastQueue.deliverToRegisteredReceiverLocked()
    BroadcastQueue.performReceiveLocked()
    	- app.thread.scheduleRegisteredReceiver()
    LoadedApk.performReceive()
    	- Handler.post(args.getRunnable())
    	- receiver.onReceive(mContext, intent);
    
    
  4. ContentProvider初始化流程

    //main
    ActivityThread.main(String[] args)
    ActivityThread.attach(false, startSeq)
    	- mgr = ActivityManager.getService(); mgr.attachApplication(mAppThread, startSeq);
    //AMS
    ActivityManagerService.attachApplication(IApplicationThread thread, long startSeq)
    ActivityManagerService.attachApplicationLocked(IApplicationThread thread,...)
    	- generateApplicationProvidersLocked()//通过PMS获取xml注册的ContentProvider组件
    	- thread.bindApplication(processName, appInfo, providerList)//将解析的provider信息传递给主进程
    //main
    ActivityThread.bindApplication()
    	- sendMessage(H.BIND_APPLICATION, data);
    ActivityThread.handleBindApplication(data);
    ActivityThread.installContentProviders(app, data.providers)
    	- for (ProviderInfo cpi : providers)
    	- installProvider(context, null, cpi,false, true, true);
    ContentProvider.attachInfo()
    	- ContentProvider.this.onCreate();	//ContentProvider的初始化是在Application之前哦。。。
    
    //main
    ContentResolver.insert()
    ContentProvider.acquireProvider()
    ActivityThread.acquireExistingProvider()//获取ContentProviderProxy代理类
    ContentProviderProxy.insert()	//binder通信mRemote.transact -> onTransact
    ContentProviderNative.insert()
    

标签:BroadcastQueue,ActivityManagerService,ActivityThread,解读,源码,Activity,Android,main
来源: https://www.cnblogs.com/yongfengnice/p/15029638.html