Android Scheme协议与应用全解析
URL Scheme 的作用
客户端应用可以向操作系统注册一个 URL Scheme,该 Scheme 用于从浏览器或其他应用中启动本应用。
通过指定的 URL 字段,可以让应用在被调起后直接打开某些特定页面,比如:书籍封面页,书城页面,原创页面,订单详情页、充值页,促销广告页等等。也可以执行某些指定动作,如订单支付等。也可以在应用内或者应用外,通过 html 页来直接调用显示 app 内的某个页面。
URL Scheme 的格式
客户端自定义的 URL 作为从一个应用调用另一个的基础,遵循 RFC 1808 (Relative Uniform Resource Locators) 标准。这跟我们常见的网页内容 URL 格式一样。
一个普通的 URL 分为几个部分,scheme、host、port、relativePath、query、fragment,
URL语法:
URL由三部分组成:资源类型、存放资源的主机域名、资源文件名。
下面举一个例子:
比如:http://www.sina.com/s?rsv_bp=1&rsv_spt=1&wd=NSurl&inputT=2709,这个URL中,scheme为 http,host为www.sina.com,relativePath为 /s,query 为 rsv_bp=1&rsv_spt=1&wd=NSurl&inputT=2709。
再列举了一个在应用中使用的 URL 例子(该 URL 会调起书籍封面页):sqreader://com.sq.controller/readbook?book_id=123456,其中 scheme 为 sqreader,host 为 com.sq.controller,relativePath 为 /readbook,query 为 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) {
// 解析SCHEME
if (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页面的锚点跳转相应的页面
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//解析scheme
if (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,否则返回false
return 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 {
@Override
protected 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) {
// 异常情况下退出当前Activity
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
- 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协议与应用全解析的更多相关文章
- Android图片载入框架最全解析(一),Glide的基本使用方法
转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/53759439 本文同步发表于我的微信公众号.扫一扫文章底部的二维码或在微信搜索 郭 ...
- 对于Android Service 生命周期进行全解析
应用程序组件有一个生命周期——一开始Android实例化他们响应意图,直到结束实例被销毁.在这期间,他们有时候处于激活状态,有时候处于非激 活状态:对于活动,对用户有时候可见,有时候不可见.组件生命周 ...
- 我的Android进阶之旅------>(全解析)屏幕尺寸,分辨率,像素,PPI之间到底什么关系?
作者:马忠信,作者授权早读课发表,转载请联系作者. 原文链接:http://www.jianshu.com/p/c3387bcc4f6e# 互联网早读课:http://zaodula.com/arc ...
- Android异步载入全解析之使用多线程
异步载入之使用多线程 初次尝试 异步.异步,事实上说白了就是多任务处理.也就是多线程执行.多线程那就会有各种问题,我们一步步来看.首先.我们创建一个class--ImageLoaderWithoutC ...
- Android异步载入全解析之IntentService
Android异步载入全解析之IntentService 搞什么IntentService 前面我们说了那么多,异步处理都使用钦定的AsyncTask.再不济也使用的Thread,那么这个Intent ...
- RTP协议全解析(H264码流和PS流)(转)
源: RTP协议全解析(H264码流和PS流)
- syslog之二:syslog协议及rsyslog服务全解析
目录: <syslog之一:Linux syslog日志系统详解> <syslog之二:syslog协议及rsyslog服务全解析> <syslog之三:建立Window ...
- Android Activity启动流程源码全解析(1)
前言 Activity是Android四大组件的老大,我们对它的生命周期方法调用顺序都烂熟于心了,可是这些生命周期方法到底是怎么调用的呢?在启动它的时候会用到startActivty这个方法,但是这个 ...
- Android Activity启动流程源码全解析(2)
接上之前的分析 ++Android Activity启动流程源码全解析(1)++ 1.正在运行的Activity调用startPausingLocked 一个一个分析,先来看看startPausing ...
随机推荐
- 【转】globk通常的应用
1.重复性分析(glred) -单独的观测期 -联合的观测期 2.联合多期数据得到基于实验的平均位置 -联合单独观测的测站 3.联合平均位置估计速度或地震偏移和震后运动 卡尔曼滤波 1.等同于序贯最小 ...
- PHP 第3方评论系统
这段时间,无觅 评论也下线不能使用了. 客户好几个网站使用了.无觅 评论,前面也是用的是多说还是什么,总之也是第3方评论,没想到没过多久,又停止使用了. 没办法,网站还是需要评论系统,一气之下,自己做 ...
- Go笔记-垃圾回收集和SetFinalizer
[垃圾回收] 1- Go的开发者也不用写代码来释放程序中不再使用的变量和结构占用内存,Go中有独立的进程,垃圾回收器(GC),处理这些事情.它会搜索不再使用的变量然后释放它们. 2- ...
- Springboot security cas整合方案-实践篇
承接前文Springboot security cas整合方案-原理篇,请在理解原理的情况下再查看实践篇 maven环境 <dependency> <groupId>org.s ...
- ZOJ 3229 Shoot the Bullet [上下界最大流]
ZOJ 3229 Shoot the Bullet 题意:此生无悔入东方 上下界最大流 spj挂掉了我也不知道对不对,把代码放这里吧以后正常了可能会评测一下 #include <iostream ...
- BZOJ 4766: 文艺计算姬 [矩阵树定理 快速乘]
传送门 题意: 给定一个一边点数为n,另一边点数为m,共有n*m条边的带标号完全二分图$K_{n,m}$ 求生成树个数 1 <= n,m,p <= 10^18 显然不能暴力上矩阵树定理 看 ...
- testng 异常 截图
testNG里有一个异常监听类,失败时会执行类里的相关方法 DriverBase 截图类 TestngListenerScreen 异常监听类 Test1 测试类1.DriverBase类 packa ...
- 不受支持的URL Statue Code 1002
1. 错误提示: Error description=Error Domain=NSURLErrorDomain Code=-1002 "unsupported URL" User ...
- 奥酷HTML5视频直播系统AMS6.0
今日,北极星通自主研发的流媒体服务系统Aoku Media Server6.0发布了,将正式支持HTML5直播,这使得网页中无需有flash播放插件或者其他插件,可直接观看直播,HTML5直播也会使得 ...
- 基于Parallax设计HTML视差效果
年关将至,给大家拜年. 最近时间充裕了一点,给大家介绍一个比较有意思的控件:Parallax.它可以用来实现鼠标移动时,页面上的元素也做偏移的视差效果.在一些有表现层次,布局空旷的页面上,用来做Hea ...