最近公司业务发展迅速,单一的项目工程再也不适合公司发展须要,因此开始推动公司APP业务组件化,很荣幸本身可以牵头作这件事,通过研究实现组件化的通讯方案经过URL Scheme,因此想着如今仍是在预研阶段,颇有必要先了解一下URL Scheme,看看是如何使用的?其实在以前作Hybrid混合编程的时候就接触过URL Scheme,总来的来讲还不算陌生,今天就来回顾总结一下。业务组件化相关博客地址(Android业务组件化之现状分析与探讨)html
业务组件化相关文章地址:android
android中的scheme是一种页面内跳转协议,是一种很是好的实现机制,经过定义本身的scheme协议,能够很是方便跳转app中的各个页面;经过scheme协议,服务器能够定制化告诉App跳转那个页面,能够经过通知栏消息定制化跳转页面,能够经过H5页面跳转页面等。编程
客户端应用能够向操做系统注册一个 URL scheme,该 scheme 用于从浏览器或其余应用中启动本应用。经过指定的 URL 字段,可让应用在被调起后直接打开某些特定页面,好比商品详情页、活动详情页等等。也能够执行某些指定动做,如完成支付等。也能够在应用内经过 html 页来直接调用显示 app 内的某个页面。综上URL Scheme使用场景大体分如下几种:浏览器
先来个完整的URL Scheme协议格式:服务器
xl://goods:8888/goodsDetail?goodsId=10011002
经过上面的路径 Scheme、Host、port、path、query所有包含,基本上平时使用路径就是这样子的。app
<activity android: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>
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); }
网页上maven
<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);
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的基本使用也就这么多了,其余的使用在之后用到的时候再作总结。post