Activity
Activity是展示型组件
startActivity will start a new activity,which will be placed at the top of the activity stack.
startActivityResult will launch an activity and would return a result when it finished.
- Activity.startActivityForResult
- Instrumentation.execStartActivity
- ActivityManagerProxy.startActivity
- ActivityManagerService.startActivity
- ActivityStack.startActivityMayWait
- ActivityStack.startActivityLocked
- ActivityStack.startActivityUncheckedLocked
- ActivityStack.resumeTopActivitiesLocked
- Activity.resumeTopActivityInnerLocked
- ActivityStack.startSpecificActivityLocked
- ActivityStack.realStartActivityLocked
- ApplicationThread.scheduleLaunchActivity
- sendMessage(LAUNCH_ACTIVITY)
- ActivityThread.handleLaunchActivity
- ActivityThread.performLaunchAcitvity
- MainActivity.onCreate
主要过程:
- Launcher通知AMS需要启动一个activity
- AMS创建一个新的进程,然后启动一个ActivityThread实例
- ActivityThread把ApplicationThread给AMS
- AMS通知ActivityThread可以启动Activity了
涉及的class
- Launcher:android启动器
- Activity:Activity类,包含一个成员变量m
- Instrumentation:主要用来监控应用程序和系统间的交互
- ActivityMangerProxy:ActivityManagerService 的代理
- ActivityStack: 描述一个activity的堆栈
- ApplicationThreadProxy:
- ApplicationThread
- ActivityThread:
- ActivityManagerService:
ActivityManager框架
ActivityManager: Interact with the overall activities running in the system.
ActivityManager在用户进程,执行操作的是在系统进程的AMS,从activityManager到ams是进程间通信,
可以类比如aidl以理解,ActivityManagerProxy是代理类,抽象类ActivityManagerNative是Stub类,IActivityManager是aidl类,ams就是service类;
ActivityManager使用的是remote proxy模式设计的,可以实现跨进程的对象访问。
这个图就是activityManager的执行的一个过程图。
ActivityStack
State and management of a single stack of activities.
activity destroy ,pause,stop,
resumeTopActivityLocked:ensure the top activity is resumed
ActivityThread
This manages the execution of the main thread in an application process, scheduling and executing activities, broadcasts, and other operations on it as the activity manager requests.
主线程主要管理,调度,执行activity和broadcast的操作。
ActivityThread即是Application的主线程,主线程的主要责任是快速处理UI事件和Broadcast消息,
注意的是,View组件由主线程执行,故onDraw是在主线程的,而surfaceView在后台线程。
android app的入口是ActivityThread的main函数,这里做了这些事:创建Looper,Handler,ActivityThread,
ActivityThread的成员变量ApplicationThread是用来与AMS通信的,通信方式是binder。
ApplicationThread
ApplicationThread继承ApplicationThreadNative,
Cast a Binder object into an application thread interface, generating a proxy if needed.
ApplicationThreadNative实现了IApplicationThread,
System private API for communicating with the application. This is given to the activity manager by an application when it starts up, for the activity manager to tell the application about things it needs to do.
service
Service是计算型组件
装饰模式的目的是为了方便扩展,可以动态的给一个对象添加一些其他的职责。
activity在startService的时候使用了这个设计模式,
activity继承子contextWrapper,wrapper里有个ContextImpl的mBase,最终会调到AcitvityManagerProxy的startService。接着通过binder,调用ams的startService,
ActivityManagerService.startService
ActivityManagerService.startServiceLocked
ActivityManagerService.bringUpServiceLocked
ActivityManagerService.startProcessLocked
Process.start
ActivityThread.main
ActivityThread.attach
ActivityManagerProxy.attachApplication
ActivityManagerService.attachApplication
ActivityManagerService.attachApplicationLocked
ActivityManagerService.realStartServiceLocked
ApplicationThreadProxy.scheduleCreateService
ApplicationThread.scheduleCreateService
ActivityThread.queueOrSendMessage
H.sendMessage
H.handleMessage
ActivityThread.handleCreateService
ClassLoader.loadClass
Obtain Service
Service.onCreate
三个步骤:
主进程调用到ams,创建新进程
新进程调用到ams,获取信息
ams调用新进程,启动服务
broadcast:
BroadcastReceiver是消息型组件
广播的好处是可以不知道对方存在,这样可以降低组件的耦合。
registerReceiver 过程分析:
AMS负责所有广播的注册和发布,
ContextWrapper.registerReceiver
ContextImpl.registerReceiver
ActivityThread.getHandler
LoadedApk.getReceiverDispatcher
ActivityManagerProxy.registerReceiver
sendBroadcast过程:
ContextWrapper.sendBroadcast
ContextImpl.sendBroadcast
ActivityManagerProxy.broadcastIntent
ActivityManagerService.broadcastIntent
ActivityManagerService.broadcastIntentLocked
ActivityManagerService.scheduleBroadcastsLocked
Handler.sendEmptyMessage
ActivityManagerService.processNextBroadcast
ActivityManagerService.deliverToRegisteredReceiverLocked
ActivityManagerService.performReceiveLocked
ApplicationThreadProxy.scheduleRegisteredReceiver
ApplicaitonThread.scheduleRegisteredReceiver
InnerReceiver.performReceive
ReceiverDispatcher.performReceive
Handler.post
Args.run
BroadcastReceiver.onReceive
ContentProvider:
ContentProvider是数据共享型组件
ContentProvider下面使用的是Binder和匿名共享内存机制。
http://blog.csdn.net/luoshengyang/article/details/6963418
ArticlesAdapter.getArticleCount
ContentResolver.acqireProvider
ApplicationContentResolve.acquireProvider
ActivityThread.acquireProvider
ActivityThread.getProvider
ActivityManagerService.getContentProvider
ActivityManagerService.getContentProviderImpl
ActivityManagerService.startProcessLocked
Process.start
ActivityThread.main
ActivityThread.attach
ActivityManagerService.attachApplication
ActivityManagerService.attachApplicationLocked
ApplicationThread.bindApplication
ActivityThread.handleBindApplication
ActivityThread.installContentProviders
ActivityThread.installProvider
ContentProvider.getIContentProvider
ContentProvider.attachInfo
ArticlesProvider.onCreate
ActivityMangerService.publishContentProviders
ActivityThread.installProvider
共享数据原理:
ContentProvider组件通过匿名共享内存机制实现,只用传输一个文件描述符即可。
http://blog.csdn.net/luoshengyang/article/details/6967204
ArticlesAdapter.getArticleByPos
ContentResolver.query
ContentResolver.acquireProvider
ApplicationContentResolver.acquireProvider
ActivityThread.acquireProvider
ActivityThread.getProvider
ContentProviderProxy.query
CursorWindow.native_init
CursorWindow.initBuffer
ContentProviderProxy.bulkQueryInternal
CursorWindow.writeToParcel
CursorWindow.native_getBinder
CursorWindow.getMemory
ContentProviderNative.onTransact
CursorWindow.CREATOR.createFromParcel
CursorWindow.native_init
CursorWindow.setMemory
Transport.bulkQuery
ArticlesProvider.query
SQLiteQueryBuilder.query
SQLiteDatabase.rawQueryWithFactory
SQLiteCursorDriver.query
AbstractWindowedCursor.setWindow
SQLiteCursor.getCount
QLiteCursor.fillWindow
SQLiteCursor.fillWindow
class :
ContentResolver
This class provides applications access to the content model.
CursorWindow:
A buffer containing multiple cursor rows.
A CursorWindow is read-write when initially created and used locally. When sent to a remote process (by writing it to a Parcel), the remote process receives a read-only view of the cursor window. Typically the cursor window will be allocated by the producer, filled with data, and then sent to the consumer for reading.