第二板块:Android 四大组件标准化学理 | 第六篇:四大组件架构总论与 Manifest 规范
所属板块:第二板块 --- Android 四大组件标准化学理
前置知识:第一板块中的 Context 体系、Binder IPC 机制、Zygote 进程孵化
本篇定位 :从 Android 系统设计者的视角,解析四大组件(Activity、Service、BroadcastReceiver、ContentProvider)的架构总纲 与 Manifest 注册规范。我们将深入探讨组件如何被系统发现、如何被调度、以及它们之间的进程间通信(IPC)边界。全程无业务实现、无生命周期回调用法、无性能优化建议,仅保留 Android Framework 的底层定义与系统级契约。
1. 核心结论先行
Android 四大组件是 Android 操作系统的应用级构建块(Application Building Blocks) 。它们不是普通的 Java 对象,而是由系统服务(System Server)托管的特殊实体。
- 组件的本质 :一个声明式入口 。应用进程本身不主动创建组件,而是向系统(PMS/AMS)声明"我能处理什么",系统再根据需要在合适的时机实例化这些组件。
- Manifest 的本质 :一个全局注册表。它告诉系统:这个 APK 包含哪些组件、需要哪些权限、运行在哪个进程、以及具备什么能力。
- 调度中心 :
ActivityManagerService (AMS)是所有组件调度的中枢,它通过 Binder 与应用的ApplicationThread通信,指挥应用进程创建或销毁组件。
2. 四大组件架构总论
2.1 组件分类与特性对比
| 组件类型 | 核心职责 | 是否常驻 | 是否独立进程 | 核心系统服务 |
|---|---|---|---|---|
| Activity | 用户界面交互 | 否(前台可见时存活) | 可配置 | ActivityManagerService |
| Service | 后台逻辑执行 | 是(除非被回收) | 可配置 | ActivityManagerService |
| BroadcastReceiver | 系统/应用事件广播 | 否(瞬时执行) | 否(默认宿主进程) | ActivityManagerService |
| ContentProvider | 数据共享与访问 | 是(进程启动时加载) | 可配置 | ActivityManagerService / PackageManagerService |
2.2 组件实例化模型
四大组件均遵循被动实例化模型:
组件实例 (Activity/Service等) 应用进程 (Zygote Fork) Android System (AMS/PMS) 组件实例 (Activity/Service等) 应用进程 (Zygote Fork) Android System (AMS/PMS) #mermaid-svg-tf17mppSbKhVnbGR{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-tf17mppSbKhVnbGR .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-tf17mppSbKhVnbGR .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-tf17mppSbKhVnbGR .error-icon{fill:#552222;}#mermaid-svg-tf17mppSbKhVnbGR .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-tf17mppSbKhVnbGR .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-tf17mppSbKhVnbGR .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-tf17mppSbKhVnbGR .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-tf17mppSbKhVnbGR .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-tf17mppSbKhVnbGR .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-tf17mppSbKhVnbGR .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-tf17mppSbKhVnbGR .marker{fill:#333333;stroke:#333333;}#mermaid-svg-tf17mppSbKhVnbGR .marker.cross{stroke:#333333;}#mermaid-svg-tf17mppSbKhVnbGR svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-tf17mppSbKhVnbGR p{margin:0;}#mermaid-svg-tf17mppSbKhVnbGR .actor{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-tf17mppSbKhVnbGR text.actor>tspan{fill:black;stroke:none;}#mermaid-svg-tf17mppSbKhVnbGR .actor-line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-tf17mppSbKhVnbGR .innerArc{stroke-width:1.5;stroke-dasharray:none;}#mermaid-svg-tf17mppSbKhVnbGR .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333;}#mermaid-svg-tf17mppSbKhVnbGR .messageLine1{stroke-width:1.5;stroke-dasharray:2,2;stroke:#333;}#mermaid-svg-tf17mppSbKhVnbGR #arrowhead path{fill:#333;stroke:#333;}#mermaid-svg-tf17mppSbKhVnbGR .sequenceNumber{fill:white;}#mermaid-svg-tf17mppSbKhVnbGR #sequencenumber{fill:#333;}#mermaid-svg-tf17mppSbKhVnbGR #crosshead path{fill:#333;stroke:#333;}#mermaid-svg-tf17mppSbKhVnbGR .messageText{fill:#333;stroke:none;}#mermaid-svg-tf17mppSbKhVnbGR .labelBox{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-tf17mppSbKhVnbGR .labelText,#mermaid-svg-tf17mppSbKhVnbGR .labelText>tspan{fill:black;stroke:none;}#mermaid-svg-tf17mppSbKhVnbGR .loopText,#mermaid-svg-tf17mppSbKhVnbGR .loopText>tspan{fill:black;stroke:none;}#mermaid-svg-tf17mppSbKhVnbGR .loopLine{stroke-width:2px;stroke-dasharray:2,2;stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-tf17mppSbKhVnbGR .note{stroke:#aaaa33;fill:#fff5ad;}#mermaid-svg-tf17mppSbKhVnbGR .noteText,#mermaid-svg-tf17mppSbKhVnbGR .noteText>tspan{fill:black;stroke:none;}#mermaid-svg-tf17mppSbKhVnbGR .activation0{fill:#f4f4f4;stroke:#666;}#mermaid-svg-tf17mppSbKhVnbGR .activation1{fill:#f4f4f4;stroke:#666;}#mermaid-svg-tf17mppSbKhVnbGR .activation2{fill:#f4f4f4;stroke:#666;}#mermaid-svg-tf17mppSbKhVnbGR .actorPopupMenu{position:absolute;}#mermaid-svg-tf17mppSbKhVnbGR .actorPopupMenuPanel{position:absolute;fill:#ECECFF;box-shadow:0px 8px 16px 0px rgba(0,0,0,0.2);filter:drop-shadow(3px 5px 2px rgb(0 0 0 / 0.4));}#mermaid-svg-tf17mppSbKhVnbGR .actor-man line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-tf17mppSbKhVnbGR .actor-man circle,#mermaid-svg-tf17mppSbKhVnbGR line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;stroke-width:2px;}#mermaid-svg-tf17mppSbKhVnbGR :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 1. 请求创建组件 (Binder IPC)2. 反射创建实例 (newInstance)3. 调用生命周期回调 (onCreate/onStart)4. 汇报状态就绪
学术定义:
- 反射创建 :系统通过
ClassLoader加载组件的.class文件,并调用其无参构造函数创建实例。这就是为什么组件不能有显式构造函数。 - 生命周期回调 :系统在组件创建后,通过 Binder 回调其生命周期方法(如
onCreate),应用进程只是被动执行。
3. Manifest 规范与解析机制
3.1 Manifest 文件结构(标准化定义)
AndroidManifest.xml 是 APK 的元数据清单。以下是其标准结构(不包含业务属性):
xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.app"
android:sharedUserId="string"
android:versionCode="integer"
android:versionName="string">
<!-- 1. 应用全局配置 -->
<application
android:name=".MyApplication"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:process="string">
<!-- 2. Activity 组件 -->
<activity
android:name=".MainActivity"
android:exported="boolean"
android:launchMode="standard|singleTop|singleTask|singleInstance"
android:taskAffinity="string">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- 3. Service 组件 -->
<service
android:name=".MyService"
android:exported="boolean"
android:process="string" />
<!-- 4. BroadcastReceiver 组件 -->
<receiver
android:name=".MyReceiver"
android:exported="boolean">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<!-- 5. ContentProvider 组件 -->
<provider
android:name=".MyProvider"
android:authorities="com.example.app.provider"
android:exported="boolean"
android:readPermission="string"
android:writePermission="string" />
</application>
<!-- 6. 权限声明 -->
<uses-permission android:name="android.permission.INTERNET" />
<permission android:name="com.example.app.permission.CUSTOM" />
</manifest>
3.2 PackageManagerService (PMS) 的解析流程
PMS 在系统启动或应用安装时解析 Manifest。
#mermaid-svg-QVLljry8p4lkTNaJ{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-QVLljry8p4lkTNaJ .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-QVLljry8p4lkTNaJ .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-QVLljry8p4lkTNaJ .error-icon{fill:#552222;}#mermaid-svg-QVLljry8p4lkTNaJ .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-QVLljry8p4lkTNaJ .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-QVLljry8p4lkTNaJ .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-QVLljry8p4lkTNaJ .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-QVLljry8p4lkTNaJ .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-QVLljry8p4lkTNaJ .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-QVLljry8p4lkTNaJ .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-QVLljry8p4lkTNaJ .marker{fill:#333333;stroke:#333333;}#mermaid-svg-QVLljry8p4lkTNaJ .marker.cross{stroke:#333333;}#mermaid-svg-QVLljry8p4lkTNaJ svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-QVLljry8p4lkTNaJ p{margin:0;}#mermaid-svg-QVLljry8p4lkTNaJ .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-QVLljry8p4lkTNaJ .cluster-label text{fill:#333;}#mermaid-svg-QVLljry8p4lkTNaJ .cluster-label span{color:#333;}#mermaid-svg-QVLljry8p4lkTNaJ .cluster-label span p{background-color:transparent;}#mermaid-svg-QVLljry8p4lkTNaJ .label text,#mermaid-svg-QVLljry8p4lkTNaJ span{fill:#333;color:#333;}#mermaid-svg-QVLljry8p4lkTNaJ .node rect,#mermaid-svg-QVLljry8p4lkTNaJ .node circle,#mermaid-svg-QVLljry8p4lkTNaJ .node ellipse,#mermaid-svg-QVLljry8p4lkTNaJ .node polygon,#mermaid-svg-QVLljry8p4lkTNaJ .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-QVLljry8p4lkTNaJ .rough-node .label text,#mermaid-svg-QVLljry8p4lkTNaJ .node .label text,#mermaid-svg-QVLljry8p4lkTNaJ .image-shape .label,#mermaid-svg-QVLljry8p4lkTNaJ .icon-shape .label{text-anchor:middle;}#mermaid-svg-QVLljry8p4lkTNaJ .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-QVLljry8p4lkTNaJ .rough-node .label,#mermaid-svg-QVLljry8p4lkTNaJ .node .label,#mermaid-svg-QVLljry8p4lkTNaJ .image-shape .label,#mermaid-svg-QVLljry8p4lkTNaJ .icon-shape .label{text-align:center;}#mermaid-svg-QVLljry8p4lkTNaJ .node.clickable{cursor:pointer;}#mermaid-svg-QVLljry8p4lkTNaJ .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-QVLljry8p4lkTNaJ .arrowheadPath{fill:#333333;}#mermaid-svg-QVLljry8p4lkTNaJ .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-QVLljry8p4lkTNaJ .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-QVLljry8p4lkTNaJ .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-QVLljry8p4lkTNaJ .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-QVLljry8p4lkTNaJ .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-QVLljry8p4lkTNaJ .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-QVLljry8p4lkTNaJ .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-QVLljry8p4lkTNaJ .cluster text{fill:#333;}#mermaid-svg-QVLljry8p4lkTNaJ .cluster span{color:#333;}#mermaid-svg-QVLljry8p4lkTNaJ div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-QVLljry8p4lkTNaJ .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-QVLljry8p4lkTNaJ rect.text{fill:none;stroke-width:0;}#mermaid-svg-QVLljry8p4lkTNaJ .icon-shape,#mermaid-svg-QVLljry8p4lkTNaJ .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-QVLljry8p4lkTNaJ .icon-shape p,#mermaid-svg-QVLljry8p4lkTNaJ .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-QVLljry8p4lkTNaJ .icon-shape .label rect,#mermaid-svg-QVLljry8p4lkTNaJ .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-QVLljry8p4lkTNaJ .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-QVLljry8p4lkTNaJ .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-QVLljry8p4lkTNaJ :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} PackageManagerService
扫描 APK 目录 (/data/app)
解析 AndroidManifest.xml
构建 Package 对象
注册组件信息 (Activity/Service/Provider/Receiver)
保存到 Settings (packages.xml)
更新系统广播 (ACTION_PACKAGE_ADDED)
解析结果存储:
PackageParser.Package:内存中的数据结构,包含所有组件信息。packages.xml:持久化存储,记录所有已安装应用的包名、版本、权限、组件列表。
4. 组件间通信与 Intent 机制
4.1 Intent 作为组件间的契约
Intent 是组件间通信的消息载体。它分为两种:
| Intent 类型 | 特点 | 解析方式 |
|---|---|---|
| 显式 Intent | 指定目标组件的类名 | 直接匹配 ComponentName |
| 隐式 Intent | 指定 Action、Category、Data | 通过 PackageManager 查询匹配的组件 |
4.2 Intent Filter 的匹配算法
当使用隐式 Intent 时,PMS 会根据 Manifest 中声明的 <intent-filter> 进行匹配。
java
// PackageManagerService 中的匹配逻辑(简化)
public List<ResolveInfo> queryIntentActivities(Intent intent, String resolvedType, int flags) {
// 1. 遍历所有已安装的 Package
// 2. 遍历 Package 中的所有 Activity
// 3. 检查 Activity 的 Intent Filter 是否匹配 Intent
// 4. 返回匹配的 Activity 列表
}
匹配规则:
- Action:Intent 的 Action 必须包含在 Filter 的 Action 列表中。
- Category:Intent 的所有 Category 必须包含在 Filter 的 Category 列表中。
- Data:Intent 的 URI 和数据类型(MIME)必须与 Filter 的 Data 规范匹配。
5. 进程模型与组件归属
5.1 组件与进程的映射关系
一个组件运行在哪个进程,由 Manifest 中的 android:process 属性决定。
| 配置方式 | 进程行为 |
|---|---|
| 不配置 | 默认运行在应用的主进程(包名进程)。 |
:remote |
运行在私有远程进程(以冒号开头,进程名 = 包名:remote)。 |
| 完整包名 | 运行在全局共享进程(多个应用可运行在同一进程,需相同签名和 sharedUserId)。 |
5.2 多进程下的组件实例化
#mermaid-svg-CQSgZfpS9FJ8iLl0{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-CQSgZfpS9FJ8iLl0 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-CQSgZfpS9FJ8iLl0 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-CQSgZfpS9FJ8iLl0 .error-icon{fill:#552222;}#mermaid-svg-CQSgZfpS9FJ8iLl0 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-CQSgZfpS9FJ8iLl0 .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-CQSgZfpS9FJ8iLl0 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-CQSgZfpS9FJ8iLl0 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-CQSgZfpS9FJ8iLl0 .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-CQSgZfpS9FJ8iLl0 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-CQSgZfpS9FJ8iLl0 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-CQSgZfpS9FJ8iLl0 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-CQSgZfpS9FJ8iLl0 .marker.cross{stroke:#333333;}#mermaid-svg-CQSgZfpS9FJ8iLl0 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-CQSgZfpS9FJ8iLl0 p{margin:0;}#mermaid-svg-CQSgZfpS9FJ8iLl0 .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-CQSgZfpS9FJ8iLl0 .cluster-label text{fill:#333;}#mermaid-svg-CQSgZfpS9FJ8iLl0 .cluster-label span{color:#333;}#mermaid-svg-CQSgZfpS9FJ8iLl0 .cluster-label span p{background-color:transparent;}#mermaid-svg-CQSgZfpS9FJ8iLl0 .label text,#mermaid-svg-CQSgZfpS9FJ8iLl0 span{fill:#333;color:#333;}#mermaid-svg-CQSgZfpS9FJ8iLl0 .node rect,#mermaid-svg-CQSgZfpS9FJ8iLl0 .node circle,#mermaid-svg-CQSgZfpS9FJ8iLl0 .node ellipse,#mermaid-svg-CQSgZfpS9FJ8iLl0 .node polygon,#mermaid-svg-CQSgZfpS9FJ8iLl0 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-CQSgZfpS9FJ8iLl0 .rough-node .label text,#mermaid-svg-CQSgZfpS9FJ8iLl0 .node .label text,#mermaid-svg-CQSgZfpS9FJ8iLl0 .image-shape .label,#mermaid-svg-CQSgZfpS9FJ8iLl0 .icon-shape .label{text-anchor:middle;}#mermaid-svg-CQSgZfpS9FJ8iLl0 .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-CQSgZfpS9FJ8iLl0 .rough-node .label,#mermaid-svg-CQSgZfpS9FJ8iLl0 .node .label,#mermaid-svg-CQSgZfpS9FJ8iLl0 .image-shape .label,#mermaid-svg-CQSgZfpS9FJ8iLl0 .icon-shape .label{text-align:center;}#mermaid-svg-CQSgZfpS9FJ8iLl0 .node.clickable{cursor:pointer;}#mermaid-svg-CQSgZfpS9FJ8iLl0 .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-CQSgZfpS9FJ8iLl0 .arrowheadPath{fill:#333333;}#mermaid-svg-CQSgZfpS9FJ8iLl0 .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-CQSgZfpS9FJ8iLl0 .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-CQSgZfpS9FJ8iLl0 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-CQSgZfpS9FJ8iLl0 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-CQSgZfpS9FJ8iLl0 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-CQSgZfpS9FJ8iLl0 .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-CQSgZfpS9FJ8iLl0 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-CQSgZfpS9FJ8iLl0 .cluster text{fill:#333;}#mermaid-svg-CQSgZfpS9FJ8iLl0 .cluster span{color:#333;}#mermaid-svg-CQSgZfpS9FJ8iLl0 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-CQSgZfpS9FJ8iLl0 .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-CQSgZfpS9FJ8iLl0 rect.text{fill:none;stroke-width:0;}#mermaid-svg-CQSgZfpS9FJ8iLl0 .icon-shape,#mermaid-svg-CQSgZfpS9FJ8iLl0 .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-CQSgZfpS9FJ8iLl0 .icon-shape p,#mermaid-svg-CQSgZfpS9FJ8iLl0 .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-CQSgZfpS9FJ8iLl0 .icon-shape .label rect,#mermaid-svg-CQSgZfpS9FJ8iLl0 .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-CQSgZfpS9FJ8iLl0 .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-CQSgZfpS9FJ8iLl0 .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-CQSgZfpS9FJ8iLl0 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} Binder IPC
Binder IPC
远程进程 (com.example.app:remote)
Service Y
ContentProvider Z
主进程 (com.example.app)
Activity A
Service X
学术定义:
- 进程隔离:不同进程的内存空间是隔离的。组件跨进程通信必须通过 Binder。
- Application 多实例 :每个进程都会创建一个独立的
Application实例。
6. 系统服务对组件的调度
6.1 ActivityManagerService (AMS) 的角色
AMS 是四大组件的总调度室。它负责:
- 组件生命周期:管理 Activity 栈、Service 生命周期。
- 进程管理:根据组件重要性调整进程优先级(oom_adj)。
- 任务栈管理:管理 Activity 的任务栈(Task)和回退栈(Back Stack)。
6.2 组件调度时序(以启动 Activity 为例)
Zygote 进程 ActivityManagerService 应用进程 Zygote 进程 ActivityManagerService 应用进程 #mermaid-svg-YiQO7cJwTMNJrPkp{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-YiQO7cJwTMNJrPkp .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-YiQO7cJwTMNJrPkp .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-YiQO7cJwTMNJrPkp .error-icon{fill:#552222;}#mermaid-svg-YiQO7cJwTMNJrPkp .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-YiQO7cJwTMNJrPkp .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-YiQO7cJwTMNJrPkp .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-YiQO7cJwTMNJrPkp .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-YiQO7cJwTMNJrPkp .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-YiQO7cJwTMNJrPkp .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-YiQO7cJwTMNJrPkp .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-YiQO7cJwTMNJrPkp .marker{fill:#333333;stroke:#333333;}#mermaid-svg-YiQO7cJwTMNJrPkp .marker.cross{stroke:#333333;}#mermaid-svg-YiQO7cJwTMNJrPkp svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-YiQO7cJwTMNJrPkp p{margin:0;}#mermaid-svg-YiQO7cJwTMNJrPkp .actor{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-YiQO7cJwTMNJrPkp text.actor>tspan{fill:black;stroke:none;}#mermaid-svg-YiQO7cJwTMNJrPkp .actor-line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-YiQO7cJwTMNJrPkp .innerArc{stroke-width:1.5;stroke-dasharray:none;}#mermaid-svg-YiQO7cJwTMNJrPkp .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333;}#mermaid-svg-YiQO7cJwTMNJrPkp .messageLine1{stroke-width:1.5;stroke-dasharray:2,2;stroke:#333;}#mermaid-svg-YiQO7cJwTMNJrPkp #arrowhead path{fill:#333;stroke:#333;}#mermaid-svg-YiQO7cJwTMNJrPkp .sequenceNumber{fill:white;}#mermaid-svg-YiQO7cJwTMNJrPkp #sequencenumber{fill:#333;}#mermaid-svg-YiQO7cJwTMNJrPkp #crosshead path{fill:#333;stroke:#333;}#mermaid-svg-YiQO7cJwTMNJrPkp .messageText{fill:#333;stroke:none;}#mermaid-svg-YiQO7cJwTMNJrPkp .labelBox{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-YiQO7cJwTMNJrPkp .labelText,#mermaid-svg-YiQO7cJwTMNJrPkp .labelText>tspan{fill:black;stroke:none;}#mermaid-svg-YiQO7cJwTMNJrPkp .loopText,#mermaid-svg-YiQO7cJwTMNJrPkp .loopText>tspan{fill:black;stroke:none;}#mermaid-svg-YiQO7cJwTMNJrPkp .loopLine{stroke-width:2px;stroke-dasharray:2,2;stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-YiQO7cJwTMNJrPkp .note{stroke:#aaaa33;fill:#fff5ad;}#mermaid-svg-YiQO7cJwTMNJrPkp .noteText,#mermaid-svg-YiQO7cJwTMNJrPkp .noteText>tspan{fill:black;stroke:none;}#mermaid-svg-YiQO7cJwTMNJrPkp .activation0{fill:#f4f4f4;stroke:#666;}#mermaid-svg-YiQO7cJwTMNJrPkp .activation1{fill:#f4f4f4;stroke:#666;}#mermaid-svg-YiQO7cJwTMNJrPkp .activation2{fill:#f4f4f4;stroke:#666;}#mermaid-svg-YiQO7cJwTMNJrPkp .actorPopupMenu{position:absolute;}#mermaid-svg-YiQO7cJwTMNJrPkp .actorPopupMenuPanel{position:absolute;fill:#ECECFF;box-shadow:0px 8px 16px 0px rgba(0,0,0,0.2);filter:drop-shadow(3px 5px 2px rgb(0 0 0 / 0.4));}#mermaid-svg-YiQO7cJwTMNJrPkp .actor-man line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-YiQO7cJwTMNJrPkp .actor-man circle,#mermaid-svg-YiQO7cJwTMNJrPkp line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;stroke-width:2px;}#mermaid-svg-YiQO7cJwTMNJrPkp :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} alt进程不存在 startActivity(intent)检查权限、解析 Intent、查找目标 Activity判断目标进程是否存在fork 新进程返回新进程 PIDscheduleLaunchActivity (Binder IPC)创建 Activity 实例、调用 onCreate
7. 关键数据结构与源码定义
7.1 ActivityRecord(AMS 中的 Activity 记录)
java
// com.android.server.am.ActivityRecord
final class ActivityRecord {
final ActivityInfo info; // 从 Manifest 解析来的信息
final String packageName;
final String processName; // 运行在哪个进程
final int userId;
TaskRecord task; // 所属的 Task
ActivityState state; // 当前状态 (INITIALIZING, RESUMED, PAUSED...)
}
7.2 ServiceRecord(AMS 中的 Service 记录)
java
// com.android.server.am.ServiceRecord
final class ServiceRecord {
final ServiceInfo info; // 从 Manifest 解析来的信息
final String packageName;
final String processName;
final int userId;
boolean isForeground; // 是否是前台服务
}
8. 本篇总结(知识闭环)
| 关键点 | 纯学术定义 |
|---|---|
| 组件的本质 | 由系统服务托管的声明式入口,而非普通对象。 |
| Manifest 的本质 | 全局注册表,定义组件属性、权限和进程归属。 |
| PMS 的作用 | 解析 Manifest,构建组件数据库,供 AMS 查询。 |
| AMS 的作用 | 四大组件的总调度室,管理生命周期、进程和任务栈。 |
| Intent 的作用 | 组件间通信的消息载体,通过匹配规则连接组件。 |
下一篇预告 :第二板块:Android 四大组件标准化学理 | 第七篇:Activity 页面载体与任务栈算法