android组件工作过程-笔记

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.

  1. Activity.startActivityForResult
  2. Instrumentation.execStartActivity
  3. ActivityManagerProxy.startActivity
  4. ActivityManagerService.startActivity
  5. ActivityStack.startActivityMayWait
  6. ActivityStack.startActivityLocked
  7. ActivityStack.startActivityUncheckedLocked
  8. ActivityStack.resumeTopActivitiesLocked
  9. Activity.resumeTopActivityInnerLocked
  10. ActivityStack.startSpecificActivityLocked
  11. ActivityStack.realStartActivityLocked
  12. ApplicationThread.scheduleLaunchActivity
  13. sendMessage(LAUNCH_ACTIVITY)
  14. ActivityThread.handleLaunchActivity
  15. ActivityThread.performLaunchAcitvity
  16. MainActivity.onCreate

主要过程:

  1. Launcher通知AMS需要启动一个activity
  2. AMS创建一个新的进程,然后启动一个ActivityThread实例
  3. ActivityThread把ApplicationThread给AMS
  4. 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.png

ActivityManager在用户进程,执行操作的是在系统进程的AMS,从activityManager到ams是进程间通信,

可以类比如aidl以理解,ActivityManagerProxy是代理类,抽象类ActivityManagerNative是Stub类,IActivityManager是aidl类,ams就是service类;

ActivityManager使用的是remote proxy模式设计的,可以实现跨进程的对象访问。

image

这个图就是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.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,761评论 5 460
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,953评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,998评论 0 320
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,248评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,130评论 4 356
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,145评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,550评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,236评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,510评论 1 291
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,601评论 2 310
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,376评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,247评论 3 313
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,613评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,911评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,191评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,532评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,739评论 2 335

推荐阅读更多精彩内容