> 編寫:[Lin-H](https://github.com/Lin-H) - 原文:[http://developer.android.com/training/app-indexing/deep-linking.html](http://developer.android.com/training/app-indexing/deep-linking.html)
為使Google能夠抓取你的app內容,并允許用戶從搜索結果進入你的app,你必須給你的app manifest中相關的[activity](# "An activity represents a single screen with a user interface.")添加intent filter。這些intent filter能使深度鏈接與你的任何[activity](# "An activity represents a single screen with a user interface.")相連。例如,用戶可以在購物app中,點擊一條深度鏈接來瀏覽一個介紹了自己所搜索的產品的頁面。
### 為你的深度鏈接添加Intent filter
要創建一條與你的app內容相連的深度鏈接,添加一個包含了以下這些元素和屬性值的intent filter到你的manifest中:
[`<action>`](http://developer.android.com/guide/topics/manifest/action-element.html)
指定[ACTION_VIEW](http://developer.android.com/reference/android/content/Intent.html#ACTION_VIEW)的操作,使得Google搜索可以觸及intent filter。
[`<data>`](http://developer.android.com/guide/topics/manifest/data-element.html)
添加一個或多個[`<data>`](http://developer.android.com/guide/topics/manifest/data-element.html)標簽,每一個標簽代表一種[activity](# "An activity represents a single screen with a user interface.")對URI格式的解析,[`<data>`](http://developer.android.com/guide/topics/manifest/data-element.html)必須至少包含[android:scheme](http://developer.android.com/guide/topics/manifest/data-element.html#scheme)屬性。
你可以添加額外的屬性來改善[activity](# "An activity represents a single screen with a user interface.")所接受的URI類型。例如,你或許有幾個[activity](# "An activity represents a single screen with a user interface.")可以接受相似的URI,它們僅僅是路徑名不同。在這種情況下,使用[android:path](http://developer.android.com/guide/topics/manifest/data-element.html#path)屬性或它的變形(`pathPattern`或`pathPrefix`),使系統能辨別對不同的URI路徑應該啟動哪個[activity](# "An activity represents a single screen with a user interface.")。
[`<category>`](http://developer.android.com/guide/topics/manifest/category-element.html)
包括[BROWSABLE](http://developer.android.com/reference/android/content/Intent.html#CATEGORY_BROWSABLE) category。[BROWSABLE](http://developer.android.com/reference/android/content/Intent.html#CATEGORY_BROWSABLE) category對于使intent filter能被瀏覽器訪問是必要的。沒有這個category,在瀏覽器中點擊鏈接無法解析到你的app。[DEFAULT](http://developer.android.com/reference/android/content/Intent.html#CATEGORY_DEFAULT) category是可選的,但建議添加。沒有這個category,[activity](# "An activity represents a single screen with a user interface.")只能夠使用app組件名稱以顯示(explicit)intent啟動。
下面的一段XML代碼向你展示,你應該如何在manifest中為深度鏈接指定一個intent filter。URI “example://gizmos” 和 “[http://www.example.com/gizmos”](http://www.example.com/gizmos”) 都能夠解析到這個[activity](# "An activity represents a single screen with a user interface.")。
~~~
<activity
android:name="com.example.android.GizmosActivity"
android:label="@string/title_gizmos" >
<intent-filter android:label="@string/filter_title_viewgizmos">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<!-- 接受以"example://gizmos”開頭的 URIs -->
<data android:scheme="example"
android:host="gizmos" />
<!-- 接受以"http://www.example.com/gizmos”開頭的 URIs -->
<data android:scheme="http"
android:host="www.example.com"
android:pathPrefix="gizmos" />
</intent-filter>
</activity>
~~~
當你把包含有指定[activity](# "An activity represents a single screen with a user interface.")內容的URI的intent filter添加到你的app manifest后,Android就可以在你的app運行時,為app與匹配URI的[Intent](http://developer.android.com/reference/android/content/Intent.html)建立路徑。
> **Note:** 對一個URI pattern,intent filter可以只包含一個單一的`data`元素,創建不同的intent filter來匹配額外的URI pattern。
學習更多關于定義intent filter,見[Allow Other Apps to Start Your ](http://developer.android.com/training/basics/intents/filters.html)[Activity](# "An activity represents a single screen with a user interface.")
### 從傳入的intent讀取數據
一旦系統通過一個intent filter啟動你的[activity](# "An activity represents a single screen with a user interface."),你可以使用由[Intent](http://developer.android.com/reference/android/content/Intent.html)提供的數據來決定需要處理什么。調用[getData()](http://developer.android.com/reference/android/content/Intent.html#getData())和[getAction()](http://developer.android.com/reference/android/content/Intent.html#getAction())方法來取出傳入[Intent](http://developer.android.com/reference/android/content/Intent.html)中的數據與操作。你可以在[activity](# "An activity represents a single screen with a user interface.")生命周期的任何時候調用這些方法,但一般情況下你應該在前期回調如[onCreate()](http://developer.android.com/reference/android/app/Activity.html#onCreate(android.os.Bundle))或[onStart()](http://developer.android.com/reference/android/app/Activity.html#onStart())中調用。
這個是一段代碼,展示如何從[Intent](http://developer.android.com/reference/android/content/Intent.html)中取出數據:
~~~
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent = getIntent();
String action = intent.getAction();
Uri data = intent.getData();
}
~~~
遵守下面這些慣例來提高用戶體驗:
-
深度鏈接應直接為用戶打開內容,不需要任何提示,插播式廣告頁和登錄頁面。要確保用戶能看到app的內容,即使之前從沒打開過這個應用。當用戶從啟動器打開app時,可以在操作結束后給出提示。這個準則也同樣適用于網站的[first click free](https://support.google.com/webmasters/answer/74536?hl=en)體驗。
-
遵循[Navigation with Back and Up](http://developer.android.com/design/patterns/navigation.html)中的設計指導,來使你的app能夠滿足用戶通過深度鏈接進入app后,向后導航的需求。
### 測試你的深度鏈接
你可以使用[Android Debug Bridge](http://developer.android.com/tools/help/adb.html)和[activity](# "An activity represents a single screen with a user interface.")管理(am)工具來測試你指定的intent filter URI,能否正確解析到正確的app [activity](# "An activity represents a single screen with a user interface.")。你可以在設備或者模擬器上運行adb命令。
測試intent filter URI的一般adb語法是:
~~~
$ adb shell am start
-W -a android.intent.action.VIEW
-d <URI> <PACKAGE>
~~~
例如,下面的命令試圖瀏覽與指定URI相關的目標app [activity](# "An activity represents a single screen with a user interface.")。
~~~
$ adb shell am start
-W -a android.intent.action.VIEW
-d "example://gizmos" com.example.android
~~~
- 序言
- Android入門基礎:從這里開始
- 建立第一個App
- 創建Android項目
- 執行Android程序
- 建立簡單的用戶界面
- 啟動其他的Activity
- 添加ActionBar
- 建立ActionBar
- 添加Action按鈕
- 自定義ActionBar的風格
- ActionBar的覆蓋層疊
- 兼容不同的設備
- 適配不同的語言
- 適配不同的屏幕
- 適配不同的系統版本
- 管理Activity的生命周期
- 啟動與銷毀Activity
- 暫停與恢復Activity
- 停止與重啟Activity
- 重新創建Activity
- 使用Fragment建立動態的UI
- 創建一個Fragment
- 建立靈活動態的UI
- Fragments之間的交互
- 數據保存
- 保存到Preference
- 保存到文件
- 保存到數據庫
- 與其他應用的交互
- Intent的發送
- 接收Activity返回的結果
- Intent過濾
- Android分享操作
- 分享簡單的數據
- 給其他App發送簡單的數據
- 接收從其他App返回的數據
- 給ActionBar增加分享功能
- 分享文件
- 建立文件分享
- 分享文件
- 請求分享一個文件
- 獲取文件信息
- 使用NFC分享文件
- 發送文件給其他設備
- 接收其他設備的文件
- Android多媒體
- 管理音頻播放
- 控制音量與音頻播放
- 管理音頻焦點
- 兼容音頻輸出設備
- 拍照
- 簡單的拍照
- 簡單的錄像
- 控制相機硬件
- 打印
- 打印照片
- 打印HTML文檔
- 打印自定義文檔
- Android圖像與動畫
- 高效顯示Bitmap
- 高效加載大圖
- 非UI線程處理Bitmap
- 緩存Bitmap
- 管理Bitmap的內存
- 在UI上顯示Bitmap
- 使用OpenGL ES顯示圖像
- 建立OpenGL ES的環境
- 定義Shapes
- 繪制Shapes
- 運用投影與相機視圖
- 添加移動
- 響應觸摸事件
- 添加動畫
- View間漸變
- 使用ViewPager實現屏幕側滑
- 展示卡片翻轉動畫
- 縮放View
- 布局變更動畫
- Android網絡連接與云服務
- 無線連接設備
- 使得網絡服務可發現
- 使用WiFi建立P2P連接
- 使用WiFi P2P服務
- 執行網絡操作
- 連接到網絡
- 管理網絡
- 解析XML數據
- 高效下載
- 為網絡訪問更加高效而優化下載
- 最小化更新操作的影響
- 避免下載多余的數據
- 根據網絡類型改變下載模式
- 云同步
- 使用備份API
- 使用Google Cloud Messaging
- 解決云同步的保存沖突
- 使用Sync Adapter傳輸數據
- 創建Stub授權器
- 創建Stub Content Provider
- 創建Sync Adpater
- 執行Sync Adpater
- 使用Volley執行網絡數據傳輸
- 發送簡單的網絡請求
- 建立請求隊列
- 創建標準的網絡請求
- 實現自定義的網絡請求
- Android聯系人與位置信息
- Android聯系人信息
- 獲取聯系人列表
- 獲取聯系人詳情
- 使用Intents修改聯系人信息
- 顯示聯系人頭像
- Android位置信息
- 獲取最后可知位置
- 獲取位置更新
- 顯示位置地址
- 創建和監視地理圍欄
- Android可穿戴應用
- 賦予Notification可穿戴特性
- 創建Notification
- 在Notifcation中接收語音輸入
- 為Notification添加顯示頁面
- 以Stack的方式顯示Notifications
- 創建可穿戴的應用
- 創建并運行可穿戴應用
- 創建自定義的布局
- 添加語音功能
- 打包可穿戴應用
- 通過藍牙進行調試
- 創建自定義的UI
- 定義Layouts
- 創建Cards
- 創建Lists
- 創建2D-Picker
- 創建確認界面
- 退出全屏的Activity
- 發送并同步數據
- 訪問可穿戴數據層
- 同步數據單元
- 傳輸資源
- 發送與接收消息
- 處理數據層的事件
- Android TV應用
- 創建TV應用
- 創建TV應用的第一步
- 處理TV硬件部分
- 創建TV的布局文件
- 創建TV的導航欄
- 創建TV播放應用
- 創建目錄瀏覽器
- 提供一個Card視圖
- 創建詳情頁
- 顯示正在播放卡片
- 幫助用戶在TV上探索內容
- TV上的推薦內容
- 使得TV App能夠被搜索
- 使用TV應用進行搜索
- 創建TV游戲應用
- 創建TV直播應用
- TV Apps Checklist
- Android企業級應用
- Ensuring Compatibility with Managed Profiles
- Implementing App Restrictions
- Building a Work Policy Controller
- Android交互設計
- 設計高效的導航
- 規劃屏幕界面與他們之間的關系
- 為多種大小的屏幕進行規劃
- 提供向下和橫向導航
- 提供向上和歷史導航
- 綜合:設計樣例 App
- 實現高效的導航
- 使用Tabs創建Swipe視圖
- 創建抽屜導航
- 提供向上的導航
- 提供向后的導航
- 實現向下的導航
- 通知提示用戶
- 建立Notification
- 當啟動Activity時保留導航
- 更新Notification
- 使用BigView風格
- 顯示Notification進度
- 增加搜索功能
- 建立搜索界面
- 保存并搜索數據
- 保持向下兼容
- 使得你的App內容可被Google搜索
- 為App內容開啟深度鏈接
- 為索引指定App內容
- Android界面設計
- 為多屏幕設計
- 兼容不同的屏幕大小
- 兼容不同的屏幕密度
- 實現可適應的UI
- 創建自定義View
- 創建自定義的View類
- 實現自定義View的繪制
- 使得View可交互
- 優化自定義View
- 創建向后兼容的UI
- 抽象新的APIs
- 代理至新的APIs
- 使用舊的APIs實現新API的效果
- 使用版本敏感的組件
- 實現輔助功能
- 開發輔助程序
- 開發輔助服務
- 管理系統UI
- 淡化系統Bar
- 隱藏系統Bar
- 隱藏導航Bar
- 全屏沉浸式應用
- 響應UI可見性的變化
- 創建使用Material Design的應用
- 開始使用Material Design
- 使用Material的主題
- 創建Lists與Cards
- 定義Shadows與Clipping視圖
- 使用Drawables
- 自定義動畫
- 維護兼容性
- Android用戶輸入
- 使用觸摸手勢
- 檢測常用的手勢
- 跟蹤手勢移動
- Scroll手勢動畫
- 處理多觸摸手勢
- 拖拽與縮放
- 管理ViewGroup中的觸摸事件
- 處理鍵盤輸入
- 指定輸入法類型
- 處理輸入法可見性
- 兼容鍵盤導航
- 處理按鍵動作
- 兼容游戲控制器
- 處理控制器輸入動作
- 支持不同的Android系統版本
- 支持多個控制器
- Android后臺任務
- 在IntentService中執行后臺任務
- 創建IntentService
- 發送工作任務到IntentService
- 報告后臺任務執行狀態
- 使用CursorLoader在后臺加載數據
- 使用CursorLoader執行查詢任務
- 處理查詢的結果
- 管理設備的喚醒狀態
- 保持設備的喚醒
- 制定重復定時的任務
- Android性能優化
- 管理應用的內存
- 代碼性能優化建議
- 提升Layout的性能
- 優化layout的層級
- 使用include標簽重用layouts
- 按需加載視圖
- 使得ListView滑動順暢
- 優化電池壽命
- 監測電量與充電狀態
- 判斷與監測Docking狀態
- 判斷與監測網絡連接狀態
- 根據需要操作Broadcast接受者
- 多線程操作
- 在一個線程中執行一段特定的代碼
- 為多線程創建線程池
- 啟動與停止線程池中的線程
- 與UI線程通信
- 避免出現程序無響應ANR
- JNI使用指南
- 優化多核處理器(SMP)下的Android程序
- Android安全與隱私
- Security Tips
- 使用HTTPS與SSL
- 為防止SSL漏洞而更新Security
- 使用設備管理條例增強安全性
- Android測試程序
- 測試你的Activity
- 建立測試環境
- 創建與執行測試用例
- 測試UI組件
- 創建單元測試
- 創建功能測試
- 術語表