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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

许进进

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值