android H5 应用内跳转Scheme协议

直接进入主题


想必大家在开发项目的过程中,都会遇到android与H5界面的交互。并且还有H5界面 跳转到APP内部的功能需要,同志们可能就会想,我们要用什么方法来做。那么接下来我们直接开撸,今天要讲解的应用内跳转Scheme协议

什么是URL Scheme

概述:
android中的scheme是一种页面内跳转协议,是一种非常好的实现机制,通过定义自己的scheme协议,可以非常方便跳转app中的各个页面;通过scheme协议,服务器可以定制化告诉App跳转那个页面,可以通过通知栏消息定制化跳转页面,可以通过H5页面跳转页面等。

URL Scheme应用场景

概述:
客户端应用可以向操作系统注册一个URL Scheme,该scheme用于从浏览器或其他应用中启动本应用,通过scheme协议来跳转到相应的APP界面,比如商品详情,活动详情,商家详情等等界面。也可以执行某些指定动作,如完成支付等。也可以在应用内部通过H5页面来直接跳转APP某个界面。

  • URL Scheme应用场景分为以下4中:

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

URL Scheme协议格式:

URL Scheme 属性分为,Scheme,Host,port,path,query,

test://shangjia/shangjiaDetail?shagnjiaId=222

通过上面的路径我们来分析,

  • scheme : test
  • host : shangjia
  • path : shangjiaDetail
  • query : shangjiaId=222

我们可以用代码来验证:
验证的地址是这个:test://start/?id=431&name=zhouyuan&age=23

Intent intent = getIntent();
    String action = intent.getAction();
    String scheme = intent.getScheme();
    Uri uri = intent.getData();
    System.out.println("action:" + action);
    System.out.println("scheme:" + scheme);
    if (uri != null) {
        String host = uri.getHost();
        String dataString = intent.getDataString();
        String id = uri.getQueryParameter("id");
        String name = String.valueOf(uri.getQueryParameters("name"));
        String age = uri.getQueryParameter("age");
        String path = uri.getPath();
        String path1 = uri.getEncodedPath();
        String queryString = uri.getQuery();
        System.out.println("host:" + host);
        System.out.println("dataString:" + dataString);
        System.out.println("id:" + id);
        System.out.println("name:" + name);
        System.out.println("age:" + age);
        System.out.println("path:" + path);
        System.out.println("path1:" + path1);
        System.out.println("queryString:" + queryString);
    }

看图

从这个例子我们能看出:我们是想跳入商家详情界面。主要的这些参数,都是需要自己去和前端去达成一个协议。并不是说非要按照我的这个例子来。比如你的项目是做汽车的,可以把URL写成

test://qiche/qicheDetail?qicheId=222

这些都没什么影响。

在AndroidMainfest.xml 配置 scheme

<application
    android:name=".MyApplication"
    android:allowBackup="true"
    android:icon="@drawable/logo"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".activity.StartActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <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:host="web"
                android:scheme="test" />
            <data
                android:host="start"
                android:scheme="test" />
        </intent-filter>

    </activity> 
<activity android:name=".activity.LoginActivity">
        <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:host="login"
                android:scheme="test" />
        </intent-filter>
    </activity>

当网页或者通知栏,Android代码发送这种规则scheme时这时候就会调起相对应界面。

那么接下来我们就该测试一波:

  1. 通过服务器下发跳转路径跳转相应页面
    button.setOnClickListener(v -> startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("test://login/123123123"))));

看下图:

这个前提,你需要在AndroidMainfest.xml配置,你看上面的代码。


2.通过H5页面的锚点跳转相应的页面

html代码:

 <html>
<head>
<title>Js调用Android</title>

</head>

<body>
<a href="test://start/?id=431&name=zhouyuan&age=23">跳转start</a>
<a href="test://web/?id=431&name=zhouyuan&age=23">跳转web</a>
<a href="test://login/?id=431&name=zhouyuan&age=23">跳转Login</a>
</body>
</html>

//webview代码
webview.setWebViewClient(new WebViewClient() {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            System.out.println("view = [" + view + "], url = [" + url + "]");
            return true;
        }
    });

  • 这个具体的我就不再讲了,我们已经获取到了URL Scheme协议地址。我们可以直接用一些正则来取到相应的字符串,然后根据该字符串来判断,跳到相应的界面。

  • 如果你觉得改方法比较麻烦。那可以直接在AndroidMainfest.xml配置相应的属性,前提是URL协议地址要相应。不然你是跳转不了的。

具体的可以自行测试,就是把 webview.setWebViewClient(new WebViewClient()){}给注释掉。
我们点击跳转Login时,会有意想不到的效果。具体还是不建议这么做,自己想一想,前面也讲到在项目中可能URL Scheme协议并不止一个界面。如果你在AndroidMainfest.xml里面去给每一个可能相关的界面都配置scheme属性,那你整个界面看着也不美观,而且还都是重复的配置。所以还是建议根据URL地址来判断跳转。


3.根据服务器下发通知栏消息,APP跳转相应的界面

其实,我们简单的想一下,都是同样的逻辑。把服务器下发的通知栏消息,里面的URL地址数据拿到,进行解析判断,然后跳转到相应的界面。具体代码就不贴了。

总结:
Android中的URL Scheme是一个非常好的机制,我们可以通过自定义Scheme属性,让我们方便,灵活的在APP内随意跳转。如果不明白请留言。

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

怀君

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

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

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

打赏作者

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

抵扣说明:

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

余额充值