Android DeepLink原理与应用(1)

来源:互联网 发布:网络电视哪个软件好用 编辑:程序博客网 时间:2024/05/22 03:24

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:

@Overridepublic 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。