0%

[Android] Create Deep Links to App Content

Introduction

何謂Deep Links:

Deep Links是指透過指定連結,來喚起指定應用,並可以向指定頁面傳送數據,它使我們應用程式之間產生了關係,讓應用不再孤立,優化了使用者體驗。

Deep Links採用Uri Scheme的方式來實現,如果不太了解Uri Scheme的人,可以查詢
維基百科


下面就簡單展示一下Uri Scheme組成:

1
2
3
4
5
6
7
8
9
10
11
   hierarchical part
┌───────────────────┴─────────────────────┐
authority path
┌───────────────┴───────────────┐┌───┴────┐
abc://username:password@example.com:123/path/data?key=value&key2=value2#fragid1
└┬┘ └───────┬───────┘ └────┬────┘ └┬┘ └─────────┬─────────┘ └──┬──┘
scheme user information host port query fragment

urn:example:mammal:monotreme:echidna
└┬┘ └──────────────┬───────────────┘
scheme path

下面我們來實做一下,如何在`Android`上實現`Deep Links`。

首先當我們在點擊連結或 Web URI 的意圖時,Android系統會依序執行以下操作,直到意圖成功被Handle

  1. 使用者指定的可以處理URI的應用程式
  2. 打開安裝的應用中,可以處理此URI的應用
  3. 允許用戶選擇可以Handle此URI的應用程式

所以為了讓URI成功被Handle,我們必須在Android專案目錄裡manifest文件下,指定activity標籤中,新增intent-filter

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<application...>

<activity ...>

<intent-filter>
<!--指定Action View的意圖操作,此intent-filter才可以從Google Search被搜尋-->
<action android:name="android.intent.action.VIEW" />

<!--類別需包含BROWSABLE,Web瀏覽器才可以訪問intent-filter-->
<category android:name="android.intent.category.BROWSABLE" />
<!--還必須包含DEFAULT,此允許應用可以響應隱式意圖,讓URI被過濾時啟動你的應用-->
<category android:name="android.intent.category.DEFAULT" />

<!--data表示每一個解析URI的格式,可以添加多個解析格式,但每一個最少要包含一個scheme屬性-->
<!-- Accepts URIs that begin with "http://nickcode4fun.net/archives” -->
<data
android:scheme="http"
android:host="nickcode4fun.net"
android:pathPrefix="/archives" />

...
<data.../>

</intent-filter>

</activity>

</application>

Read data from incoming intents

你可以在系統啟動你的Activity時,透過Intent中的getData()getAction()方法,取得與意圖相關的資訊。

1
2
3
4
5
6
7
8
9
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Intent intent = getIntent();
String action = intent.getAction();
Uri data = intent.getData();
}

運行效果如下:


那麼如何取得URL攜帶的Data呢?


Ex: http://nickcode4fun.net/archives?type=1&id=10001

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Intent intent = getIntent();
String action = intent.getAction();
Uri uri = intent.getData();
assert uri != null;
String scheme = uri.getScheme();
String host = uri.getHost();
String path = uri.getPath();
String query = uri.getQuery();
String type = uri.getQueryParameter("type");
String id = uri.getQueryParameter("id");

final String msg = "action: " + action + "\n"
+ "uri: " + uri + "\n"
+ "scheme: " + scheme + "\n"
+ "host: " + host + "\n"
+ "path: " + path + "\n"
+ "query: " + query + "\n"
+ "type: " + type + "\n"
+ "id: " + id + "\n";

}

運行效果如下:







現在Android Studio提供圖形化介面來管理Deep Links,詳情可以參考下面連結

  1. Select Tools > App Links Assistant.
  2. Click Open URL Mapping Editor and then click Add at the bottom of the URL Mapping list to add a new URL mapping.






參考資料:


Create Deep Links to App Content


Add Android App Links