[TOC]
Android的四大組件是構成安卓應用的主心骨,該四大組件均需要在AndroidManifest文件中進行注冊才能正常運作,四大組件中文翻譯過來分別是:
* 活動
* 服務
* 廣播接收器
* 內容提供器
## 檢測方法
只需要檢測AndroidManifest.xml文件中的`<activity>`,`<service>`,`<receiver>`,`<provider>`標簽中的`android:name`即可。可以參考下面是例子。
```
<application>
...
<activity></activity>
<service></service>
<receiver></receiver>
<provider></provider>
...
</application>
```

## Activity
Activity中文翻譯為活動,那么活動的概念在Android的世界中不是我們理解的平日里參加的那種`活動`,它是指一種與用戶交互的窗口。在Web前端開發中,我們知道使用HTML來繪制與用戶交互的UI界面,例如用戶進入一個淘寶商城的購物車的界面中中,點擊付款的鏈接后,進入到掃二維碼付款的界面。**簡單來說,安卓的一個活動就是Web應用中的一個界面。**
而且,安卓里的移動應用體驗與桌面版本不同,因為用戶與應用的互動并不總是在同一個地方開始。相反,用戶旅程通常是非確定性的。例如,如果您從主屏幕打開電子郵件應用程序,則可能會看到電子郵件列表。相比之下,如果您使用社交媒體應用程序然后啟動您的電子郵件應用程序,您可能會直接進入電子郵件應用程序的屏幕撰寫電子郵件。
`Activity`旨在促進這種模式。當一個應用程序調用另一個應用程序時,調用應用程序將調用另一個應用程序中的活動,而不是作為原子整體的應用程序。通過這種方式,活動可以作為應用與用戶交互的入口點。您將活動實現為`Activity`的子類。
活動為應用程序繪制UI的窗口。此窗口通常填充屏幕,也可能小于屏幕并浮動在其他窗口的頂部。通常,一個活動在應用程序中實現一個屏幕。例如,應用程序的某個活動可能會實現主屏幕,而另一個活動會實現“選擇照片”的屏幕。
大多數應用程序包含多個屏幕,這意味著它們包含多個活動。通常,應用程序中的一個活動被指定為主要活動,這是用戶啟動應用程序時顯示的第一個屏幕。然后,每個活動可以啟動另一個活動以執行不同的操作。例如,某個簡單的電子郵件程序的主活動顯示郵件的收件箱。然后把這個主活動作為起始點,可以啟動其他的活動,比如寫郵件和打開個人電子郵件等任務。
雖然活動協同工作以在應用程序中形成一個有凝聚力的用戶體驗,但每個活動只是松散地綁定到其他活動; 應用程序中的活動通常存在最小的依賴關系。實際上,活動通常會啟動屬于其他應用程序的活動。例如,瀏覽器應用可能會啟動社交媒體應用的共享活動。
語法:
```
<activity android:allowEmbedded=["true" | "false"]
android:allowTaskReparenting=["true" | "false"]
android:alwaysRetainTaskState=["true" | "false"]
android:autoRemoveFromRecents=["true" | "false"]
android:banner="drawable resource"
android:clearTaskOnLaunch=["true" | "false"]
android:configChanges=["mcc", "mnc", "locale",
"touchscreen", "keyboard", "keyboardHidden",
"navigation", "screenLayout", "fontScale",
"uiMode", "orientation", "screenSize",
"smallestScreenSize"]
android:documentLaunchMode=["intoExisting" | "always" |
"none" | "never"]
android:enabled=["true" | "false"]
android:excludeFromRecents=["true" | "false"]
android:exported=["true" | "false"]
android:finishOnTaskLaunch=["true" | "false"]
android:hardwareAccelerated=["true" | "false"]
android:icon="drawable resource"
android:label="string resource"
android:launchMode=["standard" | "singleTop" |
"singleTask" | "singleInstance"]
android:maxRecents="integer"
android:multiprocess=["true" | "false"]
android:name="string"
android:noHistory=["true" | "false"]
android:parentActivityName="string"
android:permission="string"
android:process="string"
android:relinquishTaskIdentity=["true" | "false"]
android:resizeableActivity=["true" | "false"]
android:screenOrientation=["unspecified" | "behind" |
"landscape" | "portrait" |
"reverseLandscape" | "reversePortrait" |
"sensorLandscape" | "sensorPortrait" |
"userLandscape" | "userPortrait" |
"sensor" | "fullSensor" | "nosensor" |
"user" | "fullUser" | "locked"]
android:stateNotNeeded=["true" | "false"]
android:supportsPictureInPicture=["true" | "false"]
android:taskAffinity="string"
android:theme="resource or theme"
android:uiOptions=["none" | "splitActionBarWhenNarrow"]
android:windowSoftInputMode=["stateUnspecified",
"stateUnchanged", "stateHidden",
"stateAlwaysHidden", "stateVisible",
"stateAlwaysVisible", "adjustUnspecified",
"adjustResize", "adjustPan"] >
. . .
</activity>
````
包含它的文件:`<application>`
可包含:`<intent-filter>`,`<meta-data>`
## Service
Service也就是所謂的`服務`,是一個可以在后臺執行長時間運行操作而不提供用戶界面的應用組件。服務可由其他應用組件啟動,而且即使用戶切換到其他應用,服務仍將在后臺繼續運行。此外,組件可以綁定到服務,以與之進行交互,甚至是執行進程間通信 (IPC)。 例如,服務可以處理網絡事務、播放音樂,執行文件 I/O 或與內容提供程序交互,而所有這一切均可在后臺進行。
語法:
```
<service android:description="string resource"
android:directBootAware=["true" | "false"]
android:enabled=["true" | "false"]
android:exported=["true" | "false"]
android:icon="drawable resource"
android:isolatedProcess=["true" | "false"]
android:label="string resource"
android:name="string"
android:permission="string"
android:process="string" >
. . .
</service>
```
包含它的文件:`<application>`
可包含:`<intent-filter>`,`<meta-data>`
## BroadcastReceiver
BroadcastReceiver即`廣播接收器`。Android應用可以從Android系統和其他Android應用發送或接收廣播消息,類似于[發布 - 訂閱](https://en.wikipedia.org/wiki/Publish%E2%80%93subscribe_pattern)設計模式。當感興趣的事件發生時,發送這些廣播。例如,Android系統在發生各種系統事件時發送廣播,例如系統啟動或設備開始充電時。例如,應用程序還可以發送自定義廣播,以通知其他應用程序他們可能感興趣的內容(例如,已下載了一些新數據)。
應用可以注冊以接收特定廣播。當發送廣播時,系統自動將廣播路由到已訂閱接收該特定類型廣播的應用。
一般而言,廣播可以用作跨應用程序和普通用戶流程之外的消息傳遞系統。但是,您必須小心,不要濫用機會(資源)去響應廣播,或者在后臺運行可能導致系統性能降低的作業.
語法:
```
<receiver android:directBootAware=["true" | "false"]
android:enabled=["true" | "false"]
android:exported=["true" | "false"]
android:icon="drawable resource"
android:label="string resource"
android:name="string"
android:permission="string"
android:process="string" >
. . .
</receiver>
```
包含它的文件:`<application>`
可包含:`<intent-filter>`,`<meta-data>`
## ContentProvider
ContentProvider中文可以翻譯為`內容提供程序`。可以對結構化數據集進行訪問,還可以封裝數據,并提供用于定義數據安全性的機制。內容提供程序是連接一個進程中的數據與另一個進程中運行的代碼的標準界面。
如果您想要訪問內容提供程序中的數據,可以將應用的Context中的ContentResolver對象用作客戶端來與提供程序通信。ContentResolver對象會與提供程序對象(即實現ContentProvider的類實例)通信。 提供程序對象從客戶端接收數據請求,執行請求的操作并返回結果。
如果您不打算與其他應用共享數據,則無需開發自己的提供程序。不過,您需要通過自己的提供程序在您自己的應用中提供自定義搜索建議。 如果您想將復雜的數據或文件從您的應用復制并粘貼到其他應用中,也需要創建您自己的提供程序。
Android 本身包括的內容提供程序可管理音頻、視頻、圖像和個人聯系信息等數據。
語法:
```
<provider android:authorities="list"
android:directBootAware=["true" | "false"]
android:enabled=["true" | "false"]
android:exported=["true" | "false"]
android:grantUriPermissions=["true" | "false"]
android:icon="drawable resource"
android:initOrder="integer"
android:label="string resource"
android:multiprocess=["true" | "false"]
android:name="string"
android:permission="string"
android:process="string"
android:readPermission="string"
android:syncable=["true" | "false"]
android:writePermission="string" >
. . .
</provider>
```
包含它的文件:`<application>`
可以包含:`<meta-data>`,`<grant-uri-permission>`,`<path-permission>`
## 查閱更多
更多內容請查閱Google官方文檔的說明:
1. Manifest(<https://developer.android.google.cn/guide/topics/manifest/manifest-intro>)
2. Acticity(<https://developer.android.google.cn/guide/components/activities/intro-activities>)
3. Service(<https://developer.android.google.cn/guide/components/services>)
4. Broadcast(<https://developer.android.google.cn/guide/components/broadcasts>)
5. ContentProvider(<https://developer.android.google.cn/guide/topics/providers/content-providers>)