
大家好,又见面了,我是你们的朋友全栈君。
  Deeplink是目前使用广告跟踪非常热门的一种方式,Deeplink的链接类型一般是schema://host/path?params样式。
  在Android设备中,点击Deeplink后可以打开指定应用,为了能够正确定位到需要打开的应用,并正确打开指定的Activity,需要应用开发过程中对Intent进行过滤接收进行配置(就是intent-filter),具体做法是在AndroidManifest.xml中对Activity声明的时候添加<intent-filter>的<data>节点,配置schema和一些必要的区分属性参数(如:host、path等)即可,配置的属性参数越多越详细,越能保证唯一性,准确打开需要打开的应用,而不是弹出打开应用选择框。
<intent-filter>标签包含以下属性
Intent,如果没有配置这个,Activity只能通过指定应用程序容器名称打开;也必须包含BROWSABLE,这个category允许你的intent-filter可以在Web浏览器中访问,如果没有配置这个,点击Web浏览器中的Deeplink链接将无法解析并打开Activity;<data>标签,每一个<data>标签都描述了什么样格式的URI将会分派到Activity进行处理。每一个<data>标签至少且必须包含一个android:schema属性。 
 你可以添加更多的属性来提高Activity所能接收的URI类型的精准度。举个例子:你的应用会在多个activity中接收类似的URI(相同的schema和host),但这些URI根据有这不同的路径(path),在这种情况下,使用android:path属性,或者使用路径正则表达式(pathPattern)和路径前缀(pathPrefix)变种来区分对于不同的路径,系统需要打开哪个Activity。测试的Deeplink链接:rsdkdemo://rs.com/test?referer=Deeplink_Test
Activity的配置intent-filter如下:
<activity  android:name="com.rastargame.sdk.oversea.demo.RSDemoActivity" android:configChanges="orientation|keyboardHidden|navigation|screenSize" android:launchMode="singleTop" android:screenOrientation="sensor" android:theme="@android:style/Theme.Holo.Light.NoActionBar.Fullscreen">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="rsdkdemo" android:host="rs.com" android:pathPrefix="/test"/>
    </intent-filter>
</activity>说明: 1.
<data>中的属性参数配置必须要根据Deeplink来配置,尽可能配置更多属性参数保证唯一,否则点击deeplink连接会出现选择打开应用页面。 2.如果需要在浏览器中也能打开应用,需要在intent-filter中添加<category android:name="android.intent.category.BROWSABLE" />这个配置(这个属性的含义就是在特定的情况下,可以在浏览器中打开Activity)
<action android:name="android.intent.action.MAIN" />的<intent-filter>中添加<data>标签配置会无法通过Deeplink正确打开相应页面。一个Activity是可以有多个<intent-filter>标签,所以添加另外的<intent-filter>标签进行配置;<category android:name="android.intent.category.LAUNCHER" />和<category android:name="android.intent.category.DEFAULT" />两个是相互冲突的,同时添加这两个category会导致桌面图标无法显示的问题;说明:如果你添加
<intent-filter>的Activity不包含android.intent.action.MAIN的<action>标签,就无需配置多个intent-filter。
  点击Deeplink打开应用的时候,会将Deeplink传入到应用,应用在Activity的onCreate和onNewIntent对数据就进行处理。
直接使用命令行adb测试deeplink,使用命令:
adb shell am start -a android.intent.action.VIEW -d "rsdkdemo://rs.com/test?referer=Deeplink_Test"<!DOCTYPE html>
<head>
    <meta charset="UTF-8" />
    <meta id="viewport" name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,minimal-ui">
</head>
<html>
    <input type="button" value="点击我打开Deeplink" onclick="javascrtpt:window.location.href='rsdkdemo://rs.com/test?referer=Deeplink_Test'">
</html>测试Facebook deeplink需要集成Facebook SDK,然后完成相应的配置,然后通过广告助手测试DeepLinked,测试设备上必须安装了Facebook客户端,并且登录了开发者账号对应的Facebook账号。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/147823.html原文链接:https://javaforall.cn