说明

Android中的url scheme是一种页面跳转协议,通过定义自己的scheme协议,可以实现从网页上或第三方App调用你的App

**HBuilderX自带标准真机运行基座的UrlSchemes为”hbuilder://“**,方便开发者调测

参考

https://uniapp.dcloud.net.cn/tutorial/app-android-schemes.html

uni-app项目Android离线打包UrlSchemes设置_苛学加的博客-CSDN博客

android中使用URL Scheme方式启动app_白发于樵的博客-CSDN博客

示例

App 设置 UrlSchemes

uniapp 设置 UrlSchemes

云端打包生效

字符串建议使用小写字母(不要使用特殊字符、中文等),如设置为”test”,那么其他App呼起你的app的scheme协议就是”test://“; 多个scheme使用 “,” 分割,每个字符串为一个scheme;

在 “app-plus”->”distribute”->”android” 节点的 schemes 属性配置UrlSchemes,示例如下:

1
2
3
4
5
6
7
8
9
10
"app-plus": {
"distribute": {
"android": {
"schemes": "hbuilder,myuniapp"
//...
},
//...
},
//...
},

android 设置 UrlSchemes

android 原生环境配置

android:scheme 协议类型

android:host 主机地址

android:path 具体路径

1
2
3
4
5
6
7
8
9
10
// 在activity中加入
<intent-filter>
<data
android:host="www.adc.com"
android:path="/person"
android:scheme="myapp"/>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
</intent-filter>

通过 UrlSchemes 启动 App 并传参

从网页中启动

1
<a href="myapp://www.abc.com/person?id=123&search=https://www.baidu.com/s?wd=what">start app</a>

从第三方app启动

1
2
3
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("myapp://www.abc.com/person?id=123&search=https://www.baidu.com/s?wd=what"));
startActivity(intent);

接收参数

uniapp

App.vue onShow 中接收参数

plus.runtime.arguments获取完整的urlscheme字符串

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
onLaunch: function() {
console.log('App Show');

let args= plus.runtime.arguments;
if(args && args.indexOf("thirdId") != -1){
// 处理args参数,如直达到某新页面等
console.log(args)
let thirdId = args.substring(args.indexOf("=")+1)
if(thirdId) {
uni.reLaunch({
url: '/pages/login/login?thirdId=' + thirdId
})
}
}
},

android

Activity 页面中接收参数

1
2
3
4
5
6
7
8
9
10
// 接收参数
Intent intent = getIntent();
String action = intent.getAction();
if(Intent.ACTION_VIEW.equals(action)){
Uri uri = intent.getData();
if(uri != null){
String id = uri.getQueryParameter("id"); // id="123"
String search = uri.getQueryParameter("search"); // search="https://www.baidu.com/s?wd=what"
}
}