Android DeepLink原理與應用(1)

1.什麼是DeepLink?他有什麼用?

DeepLink,是一種在移動設備上實現從Web頁面通過點擊一個鏈接直接跳轉到一個App內部指定的界面的技術。
在移動互聯網時代,Web和App之間爭了好多年了,但技術其實是爲了解決問題而生,本身無對錯,Web和App直接也在不斷融合,以便爲用戶提供最好的功能和體驗。DeepLink就是一種在Web和App之間融合的技術。
從一個鏈接點擊打開一個App的某個特殊界面,“FROM”和“TO”,對於“TO”端的App來說,顯然是一種增加跳轉的機會。而對於“FROM”而言,也就佔據了入口的位置。這就和Android的系統搜索功能的處境有點類似了。對於巨頭級的App,本身就是入口,其核心競爭力就是入口的地位,自然不願意將入口分給別人。對於中小App,這是一個增加流量的機會。

老套路,既然要看Android上的玩法,首先看看官方文檔是怎麼說的:
https://developer.android.com/training/app-indexing/deep-linking.html
這篇DeepLink使用說明很簡短,可以看到Android是通過Intent+Activity這套框架實現的拉起。相關的Category是android.intent.category.BROWSABLE。
Activity的配置以及使用如下:
AndroidManifest.xml:

<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" />
        <!-- Accepts URIs that begin with "http://www.example.com/gizmos” -->
        <data android:scheme="http"
              android:host="www.example.com"
              android:pathPrefix="/gizmos" />
        <!-- note that the leading "/" is required for pathPrefix-->
        <!-- Accepts URIs that begin with "example://gizmos” -->
        <data android:scheme="example"
              android:host="gizmos" />

    </intent-filter>
</activity>

Activity:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

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

需要注意,和使用大多數Category一樣,需要添加 android.intent.category.DEFAULT。
在Data中需要指定Uri,相當於向系統註冊,將自己的Activity與Uri關聯。使用scheme/host/pathPrefix這三個字段。

這是TO端。FROM端如何使用?有兩種方法:
(1)使用Intent跳轉

                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_VIEW);
                intent.addCategory(Intent.CATEGORY_BROWSABLE);
                intent.setData(Uri.parse("http://www.example.com/gizmos"));
                startActivity(intent);

(2)使用WebView在網頁中跳轉
用一個簡易的String網頁內容來測試,html網頁文件同理。

        WebView webView = (WebView) findViewById(R.id.web);
        final String webContent = "<!DOCTYPE html><html><body><a href=\"http://www.example.com/gizmos\">test</a></body></html>";
        webView.loadData(webContent, "text/html", null);

以上兩種方法都會喚醒系統的Activity Chooser,一般至少會有TO端的App和瀏覽器供選擇。
接下來會有另一篇從Android框架看看怎麼實現的DeepLink。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章