隐式Intent
intent-filter三个重要的标签:action、category、data
- action:规定了Intent要完成的动作(必须的)
<action android:name="com.test.action1"/>
<action android:name="com.test.action2"/>
- category:指定当前动作(action)被执行的环境,必须得有默认的category(必须的),可以再加上自己定义的
//系统默认添加的
<category android:name="android.intent.category.DEFAULT" />
//自定义的
<category android:name="com.test.category" />
- data:Intent携带的数据,一般由两部分组成mimeType和URI
mimeType:资源类型格式(可以不设定资源类型)
<!--视频-->
<data android:mimeType="video/*" />
<!--音频-->
<data android:mimeType="audio/*" />
<!--图片-->
<data android:mimeType="image/*" />
<!--文本-->
<data android:mimeType="text/*" />
<!--文件->
<data android:mimeType="application/*" />
URL:的表现形式(非必须的),数据格式:scheme:// (host) : (port) / [ (path) | (pathPrefix) | (pathPattern) ],例子:test://demo:00/deep/aaaa?type=1
scheme:协议类型,我们可以自定义,一般是项目或公司缩写,String,test
host:域名地址,String,demo
port:端口,int,00
path:访问的路径,String,/deep/aaaa
pathPrefix:访问的路径的前缀,String,/deep或/dee
pathPattern:访问路径的匹配格式,相对于path和pathPrefix更为灵活,String,/.*/aaaa
<data
android:host="demo"
android:path="/deep/aaaa"
android:scheme="test"/>
<data
android:host="demo"
android:pathPrefix="/deep"
android:scheme="test"/>
<data
android:host="demo"
android:pathPattern="/.*/aaaa"
android:scheme="test"/>
DeepLink实现
四个步骤,配置AndroidManifest
<activity
android:name=".DeepLinkActivity"
android:launchMode="singleTask">
<intent-filter>
<!-- 步骤一:添加action -->
<!-- 将数据显示给用户。这是对数据执行的最常见的操作——这是您可以对数据块使用的通用操作,以获得最合理的结果。 -->
<action android:name="android.intent.action.VIEW" />
<!-- 步骤二:添加category -->
<!-- 隐式的Intent启动Activity,必须要加上此category,否则匹配失败 -->
<category android:name="android.intent.category.DEFAULT" />
<!-- 步骤三:添加category -->
<!-- 可以从浏览器安全调用的活动必须支持此类别,必须配合data使用 -->
<category android:name="android.intent.category.BROWSABLE" />
<!-- 步骤四:添加data -->
<!-- data为隐式启动静态传递数据时使用,数据格式:<scheme> :// <host> : <port> / [ <path> | <pathPrefix> | <pathPattern> ] -->
<!-- 与H5定义的数据格式:test://demo/deep/aaaa?type=1 -->
<data
android:host="demo"
android:path="/deep/aaaa"
android:scheme="test"/>
</intent-filter>
</activity>
获取URI中的参数值
var type = intent.data?.getQueryParameter("type")
打开APP的H5代码
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Style-Type" content="text/css">
</head>
<body>
<a href="test://demo/deep/aaaa?type=1">打开DeepLink页面</a>
</body>
</html>
外部(系统)分享实现
实现外部资源文件分享至APP内
<activity
android:name=".ExternalShareActivity"
android:launchMode="singleTask">
<!-- 系统分享 单个文件 -->
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<!-- 文字类型 -->
<data android:mimeType="text/*" />
<!-- 图片类型 -->
<data android:mimeType="image/*" />
<!-- 视频类型 -->
<data android:mimeType="video/*" />
<!-- 音频类型 -->
<data android:mimeType="audio/*" />
<!-- 文件类型 -->
<data android:mimeType="application/*" />
</intent-filter>
<!-- 系统分享 多个文件 -->
<intent-filter>
<action android:name="android.intent.action.SEND_MULTIPLE" />
<category android:name="android.intent.category.DEFAULT" />
<!-- 图片类型 -->
<data android:mimeType="image/*" />
</intent-filter>
</activity>
接收页面
val action = intent.action
if (action != null) {
when (action) {
Intent.ACTION_SEND -> titleText.text = "单个系统分享"
Intent.ACTION_SEND_MULTIPLE -> titleText.text = "多个系统分享"
}
}
var type = intent.type
if (type != null) {
when {
type.startsWith("text/") -> content.text = "是文本类型"
type.startsWith("video/") -> content.text = "是视频类型"
type.startsWith("audio/") -> content.text = "是音频类型"
type.startsWith("image/") -> content.text = "是图片类型"
type.startsWith("application/") -> content.text = "是文件类型"
}
}
demo地址: 链接.