DeepLink官网上有这样的解释:
When a clicked link or programmatic request invokes a web URI intent, the Android system tries each of the following actions, in sequential order, until the request succeeds:
1. Open the user's preferred app that can handle the URI, if one is designated.
2. Open the only available app that can handle the URI.
3. Allow the user to select an app from a dialog.
Follow the steps below to create and test links to your content. You can also use the [App Links Assistant](https://developer.android.com/studio/write/app-link-indexing.html) in Android Studio to add Android App Links
翻译后的意思就是:
1.当单击链接或编程请求调用Web URI意图时,Android系统按顺序依次尝试以下每一个操作,直到请求成功为止:
2.打开用户首选的应用程序,它可以处理URI,如果指定的话。
3.打开可以处理URI的惟一可用应用程序。
允许用户从对话框中选择应用程序。
意思也就是用户可以自己写一串字符串,系统会对该字符串进行解析,然后调起注册过相应scheme的应用,如果有多个注册了,那么就会弹出对话框让用户选择。
Google官方给了一个样例:search-samples
以下根据Android官方的deep-linking的样例来说明如何使用。
<activity
android:name="com.example.android.GizmosActivity"
android:label="@string/title_gizmos" >
<intent-filter android:label="@string/filter_view_http_gizmos">
<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-->
</intent-filter>
<intent-filter android:label="@string/filter_view_example_gizmos">
<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 "example://gizmos” -->
<data android:scheme="example"
android:host="gizmos" />
</intent-filter>
</activity>
在上面有两个<intent-filter ..>这两个<intent-filter ..>只是在<data ..>上有所区别,但是官方仍然建议我们分开写。比如:
<intent-filter>
...
<data android:scheme="https" android:host="www.example.com" />
<data android:scheme="app" android:host="open.my.app" />
</intent-filter>
当注册了<intent-filter..>后,便可以在Activity的中获取其他应用传过来的intent值,具体调用如下:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent = getIntent();
String action = intent.getAction();
Uri data = intent.getData();
}
我们可以这样设置scheme来通过链接拉活app
AndroidManifest中这样写:
<activity
android:name=".activity.TestUI"
android:launchMode="singleTask"
android:theme="@style/AppTheme" >
<intent-filter>
<data
android:host="www.test.com"
android:scheme="myscheme"/>
<!-- 必须加上该项,对一段数据执行的“标准”操作-->
<action android:name="android.intent.action.VIEW"/>
<!-- 必须加上该项 -->
<category android:name="android.intent.category.DEFAULT"/>
<!-- 如果希望该应用可以通过浏览器的连接启动,则添加该项 -->
<category android:name="android.intent.category.BROWSABLE"/>
</intent-filter>
</activity>
TestUI中这样写:
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this, R.layout.activity_test);
Uri data = getIntent().getData();
if (data != null) {
String host = data.getHost();
String scheme = data.getScheme();
String authority = data.getAuthority();
XxLog.d("host=" + host + ",scheme=" + scheme + ",auth=" + authority);
String type = data.getQueryParameter("type");
String name = data.getQueryParameter("name");
String url = data.getQueryParameter("url");
XxLog.d("type=" + type + ",name=" + name + ",url=" + url);
} else
XxLog.e("data is null");
}
网页中这样写:
<!DOCTYPE html>
<html>
<head>
<title>DeepLink落地页</title>
</head>
<body>
<a href="testscheme://www.test.com?type=green&url=111&name=zhangs">启动程序测试页面</a>
</body>
</html>
开发过程中可以保存为html文件用手机浏览器打开测试效果
上线后由后台将这段代码放在后台页面
如果需要在网页中添加一条点击直接跳到应用市场的按钮,可以这样
<a href="market://details?id=com.your.packagename">打开你的app市场详情页</a>
另外有文章说可以再点击启动程序的地方加一条判断,如果2秒后还没有唤醒,那么就认为该设备上没有该app,即自动跳转到应用市场。我不是做前端的,所以就不过多分析了。
参考文章:
https://www.jianshu.com/p/d5db3d2def3b
https://blog.csdn.net/wangkeke1860/article/details/49850997
https://www.jianshu.com/p/21380058d609