背景
- 「整理」在閱讀跟「工作相關(guān)」的源碼時(shí)遇到的「常見(jiàn)」的安皱、「重要」的「數(shù)據(jù)結(jié)構(gòu)」意系,方便透徹理解自己寫(xiě)的「代碼背后」在干什么事情拱燃,代碼不在于多而在于「精」
- 嘗試把「知識(shí)」分享給更多的人碎浇,鍛煉對(duì)某個(gè)知識(shí)點(diǎn)真正理解的「能力」
是什么
- 意圖腾仅,目的
- Intent 是 Android SDK 提供的一個(gè)數(shù)據(jù)結(jié)構(gòu)乒裆,內(nèi)部記錄了即將要被執(zhí)行的操作信息,基本上是一個(gè)實(shí)體類(lèi)推励,不包含過(guò)多的業(yè)務(wù)邏輯鹤耍,只是數(shù)據(jù)載體
- 可以用于啟動(dòng) Activity 、Service验辞,發(fā)送廣播信息給需要的 BroadcastReceiver稿黄,和后臺(tái) Service 進(jìn)行通信
為什么
- 解決的是 Android Component 間「通信」「信息載體」的問(wèn)題
怎么樣
使用方法
- 啟動(dòng) Activity
// 在 java 堆上分配一個(gè) intent 對(duì)象,并指定需要啟動(dòng)的組件的類(lèi)型
Intent intent = new Intent(context, MainActivity.class);
//向 Context 對(duì)象發(fā)送 startActivity 消息
context.startActivity(intent);
// 上述代碼實(shí)際上只指定了 Intent.mComponent 字段跌造,其它字段由接下來(lái)收到該 intent 消息的對(duì)象進(jìn)行寫(xiě)入
- 啟動(dòng) Service
// 指定需要啟動(dòng)的 Service 類(lèi)的名稱杆怕,方便接下來(lái)進(jìn)行查找和實(shí)例化 Service 對(duì)象
String cls = PlayMusicService.class.getName();
// 指定包名
String pkg = context.getPackageName();
// 封裝到 ComponentName 類(lèi)中方便跨進(jìn)程傳遞這兩個(gè)信息
ComponentName component = new ComponentName(pkg, cls);
// 分配 intent 對(duì)象
Intent intent = new Intent();
// 寫(xiě)入組件名稱信息
intent.setComponent(component);
// 向 Context 對(duì)象發(fā)送 startService 消息
context.startService(intent);
- 發(fā)送廣播
// 分配一個(gè) bundle 對(duì)象, 用于記錄附加數(shù)據(jù)
Bundle bundle = new Bundle();
// 向 bundle 寫(xiě)入要告訴接收者的信息
bundle.putString("key", "value");
// 分配一個(gè) intent 對(duì)象
Intent intent = new Intent();
// 指定 action 字段壳贪,方便系統(tǒng)和接收者進(jìn)行信息過(guò)濾
intent.setAction("com.passionli.action.data");
// 向 intent 寫(xiě)入附加數(shù)據(jù)
intent.putExtras(bundle);
// 向 Context 對(duì)象發(fā)送 sendBroadcast 消息陵珍,輸入是 intent 對(duì)象
context.sendBroadcast(intent);
內(nèi)部實(shí)現(xiàn)原理
UML 類(lèi)圖
Intent 結(jié)構(gòu)體
Intent 結(jié)構(gòu)體
- Intent 實(shí)現(xiàn)了 Parcelable 接口,說(shuō)明該對(duì)象具備在多進(jìn)程間傳遞的能力
- 內(nèi)部需要往 Parcel 里面讀寫(xiě)的對(duì)象也需要實(shí)現(xiàn) Parcelable 接口违施,如 ComponentName, Bundle 等
- Bundle 類(lèi)的數(shù)據(jù)由 mMap 字段記錄
主要字段
Field | Type | Description |
---|---|---|
mAction | String | 記錄將要執(zhí)行的行為互纯,如查看聯(lián)系人、打電話等 |
mData | Uri | 以 Uri 形式記錄將要被操作的數(shù)據(jù)磕蒲,如數(shù)據(jù)庫(kù)中一個(gè)聯(lián)系人留潦、一張圖片、一段視頻等 |
例如:
Action | Data | Description |
---|---|---|
android.intent.action.VIEW | content://contacts/people/1 |
「查看」「數(shù)據(jù)庫(kù)中ID為1的聯(lián)系人」的信息 |
android.intent.action.EDIT | content://contacts/people/1 |
「編輯」「數(shù)據(jù)庫(kù)中ID為1的聯(lián)系人」的信息 |
android.intent.action.DIAL | tel:123 |
「顯示」「電話號(hào)碼123」到撥號(hào)界面 |
類(lèi)似 HTTP 協(xié)議中對(duì)「資源」進(jìn)行 GET / POST / PUT / DELETE 等操作辣往。
二級(jí)字段
Field | Type | Description |
---|---|---|
mCategories | ArraySet<String> | 記錄 action 的「附加」信息 |
mType | String | 數(shù)據(jù)的 MIME 類(lèi)型 |
mComponent | ComponentName | 指定處理該意圖的組件 |
mExtras | Bundle | 一包/捆附加信息兔院,發(fā)送給接收組件的額外的數(shù)據(jù),如打開(kāi)發(fā)送郵件界面并填充郵件的標(biāo)題和主題數(shù)據(jù)等 |
優(yōu)缺點(diǎn)
- 高度封裝 Android 進(jìn)程間通信信息站削,降低上層應(yīng)用的開(kāi)發(fā)難度
練習(xí)題
- 單步跟蹤 Intent 對(duì)象的創(chuàng)建坊萝、傳遞流程,輸出 UML 類(lèi)圖和時(shí)序圖
總結(jié)
以上是現(xiàn)階段對(duì) Intent 類(lèi)的知識(shí)點(diǎn)整理钻哩,重點(diǎn)在于數(shù)據(jù)結(jié)構(gòu)和內(nèi)部實(shí)現(xiàn)原理的理解