0
点赞
收藏
分享

微信扫一扫

Android:Scheme协议打开APP界面


我在使测试过程中,自带的浏览器也可能打不开(小米浏览器),可以试试其他浏览器或者自己写一个href请求;

测试:weixin://  这个可以掉微信,这个可以的浏览器就肯定可以

URL Scheme应用场景:

1.  服务器下发跳转路径,客户端根据服务器下发跳转路径跳转相应的页面
2.  H5页面点击锚点,根据锚点具体跳转路径APP端跳转具体的页面
3.  APP端收到服务器端下发的PUSH通知栏消息,根据消息的点击跳转路径跳转相关页面
4.  APP根据URL跳转到另外一个APP指定页面

 

URL Scheme如何使用:

1、设置Scheme

<activity
android:name=".GoodsDetailActivity"
android:theme="@style/AppTheme">
<!--要想在别的App上能成功调起App,必须添加intent过滤器-->
<intent-filter>
<!--协议部分,随便设置
xl://goods:8888/goodsDetail?goodsId=10011002
-->
<data android:scheme="xl"
android:host="goods"
android:path="/goodsDetail"
android:port="8888"/>

<!--下面这几行也必须得设置-->
<category android:name="android.intent.category.DEFAULT"/>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.BROWSABLE"/>
</intent-filter>
</activity>

2、获取Scheme跳转的参数

Uri uri = getIntent().getData();
if (uri != null) {
// 完整的url信息
String url = uri.toString();
Log.e(TAG, "url: " + uri);
// scheme部分
String scheme = uri.getScheme();
Log.e(TAG, "scheme: " + scheme);
// host部分
String host = uri.getHost();
Log.e(TAG, "host: " + host);
//port部分
int port = uri.getPort();
Log.e(TAG, "host: " + port);
// 访问路劲
String path = uri.getPath();
Log.e(TAG, "path: " + path);
List<String> pathSegments = uri.getPathSegments();
// Query部分
String query = uri.getQuery();
Log.e(TAG, "query: " + query);
//获取指定参数值
String goodsId = uri.getQueryParameter("goodsId");
Log.e(TAG, "goodsId: " + goodsId);
}

3、调用方式

h5网页调用

<a href="xl://goods:8888/goodsDetail?goodsId=10011002">打开商品详情</a>

原生调用

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("xl://goods:8888/goodsDetail?goodsId=10011002"));
startActivity(intent);

js调用 (无法打开app则跳转下载app页面)

function android(){
window.location.href = "xl://goods:8888/goodsDetail?goodsId=10011002";
window.setTimeout(function(){
window.location.href = "http://【这里市下载页面】.html";
},2000);
};

3、如何判断一个Scheme是否有效

PackageManager packageManager = getPackageManager();
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("xl://goods:8888/goodsDetail?goodsId=10011002"));
List<ResolveInfo> activities = packageManager.queryIntentActivities(intent, 0);
boolean isValid = !activities.isEmpty();
if (isValid) {
startActivity(intent);
}

参考链接:https://www.jianshu.com/p/5e997a68d57a

举报

相关推荐

0 条评论