Scheme协议【浏览器唤醒APP】

编程入门 行业动态 更新时间:2024-10-03 04:38:52

Scheme协议【<a href=https://www.elefans.com/category/jswz/34/1770995.html style=浏览器唤醒APP】"/>

Scheme协议【浏览器唤醒APP】

什么是 URL Scheme?

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

URL Scheme应用场景:

  • 客户端应用可以向操作系统注册一个 URL scheme,该 scheme 用于从浏览器或其他应用中启动本应用。通过指定的 URL 字段,可以让应用在被调起后直接打开某些特定页面,比如商品详情页、活动详情页等等。也可以执行某些指定动作,如完成支付等。也可以在应用内通过 html 页来直接调用显示 app 内的某个页面。综上URL Scheme使用场景大致分以下几种:

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

URL Scheme协议格式:

先来个完整的URL Scheme协议格式:

xl://goods:8888/goodsDetail?goodsId=10011002

绝大多数的路径几乎都是 [scheme]://[host]:[8000][path]?[query]

通过上面的路径 Scheme、Host、port、path、query全部包含,基本上平时使用路径就是这样子的。

  • xl代表该Scheme 协议名称
  • goods代表Scheme作用于哪个地址域
  • goodsDetail代表Scheme指定的页面
  • goodsId代表传递的参数
  • 8888代表该路径的端口号

URL Scheme如何使用:

定义一个Schema

  • 在AndroidManifest.xml中对<activity />标签增加<intent-filter />设置Scheme
<activityandroid:name=".GoodsDetailActivity"android:theme="@style/AppTheme"><!--要想在别的App上能成功调起App,必须添加intent过滤器--><intent-filter><!--协议部分,随便设置--><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>
  • 获取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);
}
  • 调用方式
网页上
<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);

如何判断一个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);
}

 

更多推荐

Scheme协议【浏览器唤醒APP】

本文发布于:2024-02-28 01:34:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1767302.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:浏览器   协议   Scheme   APP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!