Android应用程序组件
最后更新于:2022-04-02 05:01:06
#### **Android应用程序的一般架构**
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/403b3a15a93f52469a256983835adf57_814x518.jpg)
#### **Android应用程序的一般架构**
* Activity -- UI、交互
* Service -- 后台计算
* Broadcast Receiver -- 广播
* Content Provider -- 数据
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/229a00062b0dd89ef23b9f3ccd6180b4_925x462.png)
#### **Activity生命周期**
由ActivityManagerService管理
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/5f620cd5ed7ba1f5d5224f05dbc0ec65_451x575.jpg)
#### **Activity堆栈**
由ActivityManagerService维护
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/dc3e75f0c05c893cc641be28bed9926c_619x201.jpg)
#### **Activity在堆栈中以Task的形式聚集在一起**
**Task由一系列相关的Activity组成,描述用户完成某一个操作所需要的Activity
当我们从Launcher上点击一个应用图标的时候,就启动一个Task
Task是用Android多任务的一种体现
http://developer.android.com/guide/components/tasks-and-back-stack.html**
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/111d6baea5440ff27d19e8ab639d8e9c_300x171.jpg)
#### **Service**
* Unbounded service
* Bounded service
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/d9cc669283608a4df2c5b0bdc595c83c_406x512.jpg)
#### **Broadcast Receiver**
* 注册
* 静态 -- AndroidManifest.xml
* 动态 -- Context.registerReceiver
* 广播
* 无序 -- Context.sendBroadcast
* 有序 -- Context.sendOrderedBroadcast
* **注册广播**
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/e3ca3b92735f0d6505aec285aee1f98b_651x268.png)
* **发送广播**
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/a02a367c95193289e3262e37ffcc7ded_986x335.png)
#### **Content Provider**
* 通过URI来描述
* 数据访问接口
* 数据更新机制
* **Content Provider的URI结构**
* A -- Scheme
* B -- Authority
* C -- Resource Path
* D -- Resource ID
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/5ae216e8926e46d551fab5462013e805_537x84.png)
* **Content Provider数据访问接口**
* Insert
* Update
* Delete
* Query
* Call -- Hidden
* **Content Provider数据更新机制**
* 注册内容观察者 -- ContentResolver.ContentObserver
* 发送数据更新通知 -- ContentResolver.notifyChange
* **注册Content Provider的内容观察者**
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/056b69744fe6a7c5cd69efca73f79b28_589x252.png)
* **发送Content Provider数据更新通知**
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/63cea9d69df6705e75716fdbd9856693_802x247.png)
';