URL Scheme 的作用

客户端应用可以向操作系统注册一个 URL Scheme,该 Scheme 用于从浏览器或其他应用中启动本应用。

通过指定的 URL 字段,可以让应用在被调起后直接打开某些特定页面,比如:书籍封面页,书城页面,原创页面,订单详情页、充值页,促销广告页等等。也可以执行某些指定动作,如订单支付等。也可以在应用内或者应用外,通过 html 页来直接调用显示 app 内的某个页面。

URL Scheme 的格式

客户端自定义的 URL 作为从一个应用调用另一个的基础,遵循 RFC 1808 (Relative Uniform Resource Locators) 标准。这跟我们常见的网页内容 URL 格式一样。

一个普通的 URL 分为几个部分,schemehost、port、relativePathquery、fragment, 

URL语法:

URL由三部分组成:资源类型、存放资源的主机域名、资源文件名。

URL的一般语法格式为:
(带方括号[]的为可选项):
protocol :// hostname[:port] / path / [;parameters][?query]#fragment
格式说明:
protocol(协议)
指定使用的传输协议,下表列出 protocol 属性的有效方案名称。 最常用的是HTTP协议,它也是目前WWW中应用最广的协议。
file 资源是本地计算机上的文件。格式file:///,注意后边应是三个斜杠。
ftp 通过 FTP访问资源。格式 FTP://
gopher 通过 Gopher 协议访问该资源。
http 通过 HTTP 访问该资源。 格式 HTTP://
https 通过安全的 HTTPS 访问该资源。 格式 HTTPS://
mailto 资源为电子邮件地址,通过 SMTP 访问。 格式 mailto:
MMS 通过 支持MMS(流媒体)协议的播放该资源。(代表软件:Windows Media Player)格式 MMS://
ed2k 通过 支持ed2k(专用下载链接)协议的P2P软件访问该资源。(代表软件:电驴) 格式 ed2k://
Flashget 通过 支持Flashget:(专用下载链接)协议的P2P软件访问该资源。(代表软件:快车) 格式 Flashget://
thunder 通过 支持thunder(专用下载链接)协议的P2P软件访问该资源。(代表软件:迅雷) 格式 thunder://
news 通过 NNTP 访问该资源。
hostname(主机名)
是指存放资源的服务器的域名系统(DNS) 主机名或 IP 地址。有时,在主机名前也可以包含连接到服务器所需的用户名和密码(格式:username:password@hostname)。
port(端口号)
整数,可选,省略时使用方案的默认端口,各种传输协议都有默认的端口号,如http的默认端口为80。如果输入时省略,则使用默认端口号。有时候出于安全或其他考虑,可以在服务器上对端口进行重定义,即采用非标准端口号,此时,URL中就不能省略端口号这一项。
path(路径)
由零或多个“/”符号隔开的字符串,一般用来表示主机上的一个目录或文件地址。
parameters(参数)
这是用于指定特殊参数的可选项。
query(查询)
可选,用于给动态网页(如使用CGI、ISAPI、PHP/JSP/ASP/ASP。NET等技术制作的网页)传递参数,可有多个参数,用“&”符号隔开,每个参数的名和值用“=”符号隔开。
fragment(信息片断)
字符串,用于指定网络资源中的片断。例如一个网页中有多个名词解释,可使用fragment直接定位到某一名词解释。

下面举一个例子:

比如:http://www.sina.com/s?rsv_bp=1&rsv_spt=1&wd=NSurl&inputT=2709,这个URL中,scheme为 httphostwww.sina.comrelativePath为 /squery 为 rsv_bp=1&rsv_spt=1&wd=NSurl&inputT=2709

再列举了一个在应用中使用的 URL 例子(该 URL 会调起书籍封面页):sqreader://com.sq.controller/readbook?book_id=123456,其中 scheme 为 sqreaderhost 为 com.sq.controllerrelativePath 为 /readbookquery 为 book_id=123456

在AndroidManifest.xml中设置了data属性,data代表数据源,是中最复杂的标签,因为不同的Activity支持的数据来源和类型多种多样,所以需要通过详细的data标签信息来指明。

Data的语法如下:

<data android:host="string"android:mimeType="string"android:path="string"android:pathPattern="string"android:pathPrefix="string"android:port="string"android:scheme="string" />Uri的格式:scheme://host:port/path or pathPrefix or pathPattern如果scheme没有指定,那其它的属性均无效;如果host没有指定,那么port,path,pathPrefix,pathPattern均无效;如果在manifest里这样写:<data android:scheme="something" android:host="project.example.com" />那么Uri uri = Uri.parse("something://project.example.com"); 才可以匹配再如:
<data android:scheme="something" android:host="project.example.com" android:port="80"/>
等同于这样写:
<data android:scheme="something"/>
<data android:host="project.example.com"/>
<data android:port="80"/>
那么Uri uri = Uri.parse("something://project.example.com:80"); 才可以匹配可以有多个data,只需匹配其中一个即可
<activity android:name=".MyActivityTwo" android:label="@string/activityTwo">
<intent-filter><action android:name="android.intent.action.leo"></action><category android:name="android.intent.category.DEFAULT"></category><data android:scheme="x-id"/><data android:scheme="something"/>
</intent-filter>
</activity>Intent in = new Intent();
in.setAction("android.intent.action.leo");
in.addCategory(Intent.CATEGORY_DEFAULT);            in.setData(Uri.parse("something:"));//或者用这个亦可in.setData(Uri.parse("x- id:"));
startActivity(in);  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42

Scheme定义Activity

当我们通过Intent启动一个Activity的时候,一般分为显式跳转和隐式跳转,隐式跳转我们可以通过setAction方法就可以实现,但有时我们需要通过设置URi的方式来进行页面的跳转,隐式启动则是不明确指定启动哪个Activity或者Service,而是通过设置Action、Data、Category,让系统来筛选出合适的目标。

例如:拨打电话:

Intent intent = new Intent(Intent.ACTION_DIAL,Uri.parse(“tel:10086”));
startActivity(intent);
  • 1
  • 2
  • 1
  • 2

系统接收到隐式启动请求后,会根据系统中各个Activity在AndroidManifest.xml文件中声明的来比较和判断是否匹配当前的Intent请求的。

1)在Androidmanifest.xml中定义scheme

        <!-- scheme协议 --><activity
            Android:name=".UI.translate.MyAppActivity"Android:label="@string/app_name"><!-- 要想在别的App上能成功调起App,必须添加intent过滤器 --><intent-filter><!-- 协议部分,随便设置 --><data Android:scheme="sqreader"  /><!-- 下面这几行也必须得设置 --><category Android:name="Android.intent.category.DEFAULT" />                <!-- 设置了BROWSABLE属性后,从外部html页面就可以唤起当前配置的页面了 --><category Android:name="Android.intent.category.BROWSABLE" /><action Android:name="Android.intent.action.VIEW" /></intent-filter></activity>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

这样我们便定义了能够接受scheme请求的activity实例,当网页或者是Android代码发送这种规则Scheme的请求的时候就能够吊起MyAppActivity了。

2)当然就是实现MyAppActivity

/*** Created by Charlies*/
public class MyAppActivity extends Activity{public String tag = "MyAppActivity";public Activity mContext = null;public void onCreate(Bundle b){super.onCreate(b);mContext = this;Uri uri = getIntent().getData();if (uri != null){List<String> pathSegments = uri.getPathSegments();String uriQuery = uri.getQuery();Intent intent;if (pathSegments != null && pathSegments.size() > 0) {// 解析SCHEMEif (someif) {dosomething();}else {// 若解析不到Scheme,则关闭MyAppActivity;finish();}} else {finish();}} else {finish();}}}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

MyAppActivity这个类中主要用于实现对scheme的解析,然后做出相应的动作,比如请求scheme跳转登录页面,我们可以这样定义

sqreader://com.sq.controller/readbook
  • 1
  • 1

然后我们解析出scheme如果是这样的结构就跳转登录页面。。。

这里简单说一下,我们可以通过Intent对象获取调用的scheme的host等信息

this.getIntent().getScheme();//获得Scheme名称
this.getIntent().getDataString();//获得Uri全部路径 
  • 1
  • 2
  • 1
  • 2

3)通过服务器下发跳转路径跳转相应页面

startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("sqreader://com.sq.controller/readbook")));
  • 1
  • 1

这里的”sqreader://com.sq.controller/readbook”就是服务器下发的跳转路径,当我们执行startActivity的时候就会调起MyAppActivity,然后我们通过在MyAppActivity解析Scheme的内容,跳转相应的页面。

4)通过在H5页面的锚点跳转相应的页面

@Overridepublic boolean shouldOverrideUrlLoading(WebView view, String url) {//解析schemeif (url.indexOf(H5Constant.SCHEME) != -1) {try {Uri uri = Uri.parse(url);String[] urlSplit = url.split("\\?");Map<String, String> queryMap = new HashMap<String, String>();String h5Url = null;if (urlSplit.length == 2) {queryMap = H5Constant.parseUriQuery(urlSplit[1]);h5Url = queryMap.get(H5Constant.MURL);}// 跳转MyAppActivity解析{// 若设置刷新,则刷新页面if (queryMap.containsKey(H5Constant.RELOADPRE) && "1".equals(queryMap.get(H5Constant.RELOADPRE))) {h5Fragment.isNeedFlushPreH5 = true;}Intent intent = new Intent(Intent.ACTION_VIEW, uri);h5Activity.startActivityForResult(intent, H5Constant.h5RequestCode);}} catch (Exception e) {e.printStackTrace();}return true;}// 打电话else if (url.indexOf("tel://") != -1) {final String number = url.substring("tel://".length());Config.callPhoneByNumber(h5Activity, number);return true;} else if (url.indexOf("tel:") != -1) {final String number = url.substring("tel:".length());Config.callPhoneByNumber(h5Activity, number);return true;}// 其他跳转方式else {view.loadUrl(url);//如果不需要其他对点击链接事件的处理返回true,否则返回falsereturn false;}}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44

可以发现我们为Webview设置了WebViewClient,并重写了WebViewClient的shouldOverrideUrlLoading方法,然后我们解析锚点的URL,并根据解析的内容调起MyAppActivity的Scheme Activity,然后在MyAppActivity中解析Scheme的内容并跳转相应的页面。

5)根据服务器下发通知栏消息,App跳转相应的页面

public class NotificationActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);L.i("接收到通知点击事件...");Intent realIntent = getIntent().getParcelableExtra(NotifeConstant.REAL_INTENT);// 解析scheme并跳转gotoRealScheme(this, realIntent);}/*** notification中跳转SCHEME,根据有效时间判断跳转URL地址*  跳转之后更具网络请求判断用户当前状态*/private void gotoRealScheme(Context context, Intent realIntent) {if (realIntent == null || context == null) {finish();return;}try {Log.i("开始解析通知中的参数...");long startShowTime = realIntent.getLongExtra(NotifeConstant.START_SHOW_TIME, 0);// 有效期时间,单位:s(秒)long validTime = realIntent.getLongExtra(NotifeConstant.VALID_TIME, 0);long currentTime = System.currentTimeMillis();String validActionUrl = realIntent.getStringExtra(NotifeConstant.VALID_ACTION_URL);String invalidActionUrl = realIntent.getStringExtra(NotifeConstant.INVALID_ACTION_URL);Intent schemeIntent;Log.i("开始根据URL构建Intent对象...");if ((currentTime - startShowTime) / 1000L <= validTime) {schemeIntent = H5Constant.buildSchemeFromUrl(validActionUrl);} else {schemeIntent = H5Constant.buildSchemeFromUrl(invalidActionUrl);}if (schemeIntent != null) {// 设置当前页面为通知栏打开Config.isNotificationOpen = true;context.startActivity(schemeIntent);finish();//对通知栏点击事件统计MobclickAgent.onEvent(context, UMCountConstant.PUSH_NOTIFICATION_CLICK);} else {finish();}} catch (Exception e) {// 异常情况下退出当前Activityfinish();}}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52

服务器下发的所有的通知都是先跳转这里的NotificationActivity,然后在这里执行跳转其他Activity的逻辑,而这里的H5Constant的buildSchemeFromUrl方法就是构造跳转页面Intent对象的,我们可以看一buildSchemeFromUrl方法的具体实现:

/*** 从scheme的url中构建出Intent,用于界面跳转** @param url* @return*/public static Intent buildSchemeFromUrl(String url) {if (url != null && url.indexOf(H5Constant.SCHEME) != -1) {Uri uri = Uri.parse(url);String[] urlSplit = url.split("\\?");Map<String, String> queryMap = new HashMap<String, String>();String h5Url = null;if (urlSplit.length == 2) {queryMap = H5Constant.parseUriQuery(urlSplit[1]);h5Url = queryMap.get(H5Constant.MURL);}Intent intent = new Intent(Intent.ACTION_VIEW, uri);if (!TextUtils.isEmpty(h5Url)) {intent.putExtra(H5Constant.MURL, h5Url);}return intent;}return null;}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

这样我们就搞构造除了跳转MyAppActivity的Intent对象,并将Scheme字符串传递给了MyAppActivity,这样在MyAppActivity中就可以解析Scheme字符串并执行相应的跳转逻辑了。

总结: 
    Android中的Scheme是一种非常好的实现机制,通过定义自己的Scheme协议,可以非常方便跳转App中的各个页面; 
通过Scheme协议,服务器可以定制化告诉App跳转那个页面,可以通过通知栏消息定制化跳转到指定的页面,也可以通过H5页面中的链接,唤起APP中的指定页面等。一句话,Scheme是一个解决应用内页面跳转,外部调起应用内指定页面的一种非常好的解决方案。

Android Scheme协议与应用全解析相关推荐

  1. Android 5.0系统特性全解析

    Android 5.0 Lollipop是今年最为期待的产品升级之一.它将带来全新的设计语言,更多人性化的功能,以及最纯正的Google味道. 最近Google陆续发布的Inbox.新版Gmail和今 ...

  2. android 启动app过程,Android P APP冷启动过程全解析(之四)

    经历了前面的三个阶段,activity终于初始化完毕,终终终于开始显示了和接收事件了!这就是本阶段所要做的工作: 15.新建DecorView 16.新建ViewRootImpl 17.添加到Disp ...

  3. Android控件之TextView全解析

    前言 大家好!在前几篇文章里,我们详细介绍了Android中的常用布局,使大家对Android中的页面布局有了一定认识,而对于布局中使用的一些UI控件如Button.TextView等,有的读者可能还 ...

  4. SVN+SSH协议工作方式全解析,以Sourceforge为例讲解如何在Windows下配置TortoiseSVN和Su

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! 简单地说 ...

  5. Android自定义控件系列——Paint类全解析

    Paint 常量 常量名 作用 ANTI_ALIAS_FLAG 抗锯齿标志 DITHER_FLAG 防抖动标志 EMBEDDED_BITMAP_TEXT_FLAG 绘制标记,在绘制文本时使用位图字体. ...

  6. SPI硬件设计、协议、速率全解析

    SPI硬件电路 SPI(Serial Peripheral Interface)是一种嵌入式产品常用的板级高速.全双工通信总线. 它采用1主多从的方式进行通信. 整个通信的物理层通常需要(3+N)根线 ...

  7. 我的Android进阶之旅------(全解析)屏幕尺寸,分辨率,像素,PPI之间到底什么关系?...

    作者:马忠信,作者授权早读课发表,转载请联系作者. 原文链接:http://www.jianshu.com/p/c3387bcc4f6e#  互联网早读课:http://zaodula.com/arc ...

  8. Android通过Scheme协议打开APP界面

    转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/123238777 本文出自[赵彦军的博客] 文章目录 浏览器为什么能唤起App的页面 ...

  9. android h5页面跳转,android H5 应用内跳转Scheme协议

    什么是URL Scheme 概述: android中的scheme是一种页面内跳转协议,是一种非常好的实现机制,通过定义自己的scheme协议,可以非常方便跳转app中的各个页面:通过scheme协议 ...

  10. Android 文件读写最全解析

    本文目录 Android文件读写概述 读取raw目录文件 读取assets目录文件 data/data/(包名) 目录文件读写 写数据 读数据 sdcard文件读写 申请动态权限 写数据 读数据 sd ...

最新文章

  1. except but
  2. linux git gui使用教程,跨平台最好用的Git GUI工具gitkraken
  3. 面试题: 数据库 真实面试题已看1 操作语句 存储过程 挺好 sql语句练习 有用
  4. [原]第一次遭遇Oracle的Bug,纪念一下 |ORA-00600 kmgs_pre_process_request_6|
  5. Struts2中的OGNL详解
  6. maven3安装和使用笔记
  7. leetcode90. 子集 II
  8. php 跳转qq群代码_邪少xml论坛qqxml代码—QQ音乐可播放框架QQ群任意跳转个人网站链接引流...
  9. 【python】isinstance可以接收多个类型,hasattr,getattr,setattr
  10. CE安装时的报错处理
  11. dataformatstring(DataFormatString 文本)
  12. 量子计算(三):有哪些机构或公司参与量子计算的研发
  13. 金融借贷中的风险控制
  14. 高频故障-office背景有水印的解决方案
  15. 距离拿下千亿市场,AR眼镜还缺什么?
  16. UNICODE编码转换为ASCII编码
  17. 微服务项目部署服务器,第3章 3.2 部署服务器 - 编排多个微服务
  18. c语言cfree编程步骤,C语言初探之利用C-Free编写C语言
  19. python如何计算环比增长率
  20. web动画(Animation) - 过渡效果transition

热门文章

  1. 我和刘强东的故事2:这次是真的
  2. 没有人能拒绝这个网站,没有人!
  3. 电脑散热风扇轴承分类
  4. 微信公众号开发文档,微信小程序开发文档,微信扫码支付文档,微信委托代扣模式开发文档
  5. snagit 注册码
  6. 红外线解码 VS1838B接头 12864液晶 51单片机 合众之星万能遥控器
  7. jspx格式手机打开,jspx来自埃及的java web快速开发框架
  8. 了解CV和RoboMaster视觉组(五)滤波器、观测器和预测方法:维纳滤波器Wiener Filter,LMS
  9. 数据库实验一:创建数据库和表
  10. Adobe Creative Cloud 不能加载APP导致卸载ps失败 解决方案