一、Activity的启动流程
startActivity
|
startActivityForResult
|
mInstrumentation.execStartActivity
|
ActivityManager.getService().startActivity(即调用AMS的一个单例对象的startActivity方法)
|
ams.startActivityAsUser
|
mActivityStarter.startActivityMayWait
|
mActivityStarter.startActivityLocked
|
mActivityStarter.startActivityUnchecked
|
...
|
ActivityThread.main
|
attach(绑定应用到AMS,保存ApplicationThread作为ActivityThread在AMS的代理对象)
|
ActivityThrea.handleLaunchActivity
|
ActivityThread.performLaunchActivity(通过ClassLoader创建Activity实例和Application实例,依次调用Application的attachBaseContext、onCreate、Activity的attach(创建window)、onCreate、onStart)
二、Service的工作过程
1、startService的代码调用流程
startService
|
ContextImpl.startService
|
ContextImpl.startServiceCommon
|
ActivityManager.getService().startService
|
mServices.startServiceLocked
|
mServices.startServiceInnerLocked
|
mServices.bringUpServiceLocked
|
mServices.realStartServiceLocked
|
app.thread.scheduleCreateService(app.thread即ApplicationThread)
|
ActivityThread.handleCreateService(通过ClassLoader创建service实例并执行onCreate方法)
ActivityThread.handleServiceArgs(调用Service的onStartCommand方法)
2、bindService的代码调用流程
bindService
|
ContextImpl.bindService
|
ContextImpl.bindServiceCommon
|
ActivityManager.getService().bindService
|
mServices.bindServiceLocked
|
mServices.bringUpServiceLocked
|
mServices.realStartServiceLocked
|
app.thread.scheduleCreateService
|
同startService
|
mServices.requestServiceBindingLocked
|
r.app.thread.scheduleBindService
|
ActivityThread.handleBindService(回调onBind方法)
|
ActivityManager.getService().publishService
|
(AMS)mServices.publishServiceLocked
|
c.conn.connected(c.conn的类型是ServiceDispatcher.InnerConnection)
|
ServiceDispatcher.connected
|
ActivityThread.post(new RunConnection)(RunConnection的run方法调用了ServiceDispatcher的doConnected方法,doConnected方法调用ServiceConnection的onServiceConnected方法)
三、广播的工作过程
1、广播的静态注册
应用安装时由系统自动完成注册,和其它三大组件一样是通过PMS来完成注册过程的
2、广播的动态注册
registerReceiver
|
ContextImpl.registerReceiver
|
ContextImpl.registerReceiverInternal(IIntentReceiver是对BroadcastReceiver的封装,是一个Binder接口,便于在跨进程中传递)
|
ActivityManager.getService().registerReceiver(和Service相似,通过ams完成相应的注册工作)
|
mRegisteredReceivers.put(receiver.asBinder(), rl)
BroadcastFilter bf = new BroadcastFilter(filter, rl, callerPackage,permission, callingUid, userId, instantApp, visibleToInstantApps);
rl.add(bf);
最终把InnerReceiver和IntentFilter保存起来,就完成了注册
3、广播的发送和接收
sendBroadcast
|
ContextImpl.sendBroadcast
|
ActivityManager.getService().broadcastIntent
|
(AMS)broadcastIntentLocked
// By default broadcasts do not go to stopped apps.(默认不发送广播给处于停止状态的App)
intent.addFlags(Intent.FLAG_EXCLUDE_STOPPED_PACKAGES);
|
//根据intent-filter查找出匹配的广播接受者并经过一系列的条件过滤,最终将满足条件的广播接收器添加进BroadcastQueue中,再通过BroadcastQueue发送广播
BroadcastQueue queue = broadcastQueueForIntent(intent);
BroadcastRecord r = new BroadcastRecord(queue, ...);
queue.scheduleBroadcastsLocked();
|
mHandler.sendMessage(mHandler.obtainMessage(BROADCAST_INTENT_MSG, this));
|
processNextBroadcast
|
deliverToRegisteredReceiverLocked
|
performReceiveLocked
|
app.thread.scheduleRegisteredReceiver(app.thread指得是ApplicationThread)
|
LoaderApk.ReceiverDispatcher.performReceive
|
mActivityThread.post(args.getRunnable())
|
args.run
|
receiver.onReceive(mContext, intent);
4、ContentProvider的工作过程
ContentProvider会在先于Application的onCreate创建并执行其onCreate方法
ActivityThread.main
|
thread.attach(false);
|
ActivityManager.getService().attachApplication(mAppThread)
|
(ApplicationThread)mAppThread.bindApplication
|
ActivityThread.handleBindApplication
|
installContentProviders
网友评论