Android 内部跳转URi协议的定义和实现方案
2016-11-24 10:51
337 查看
URI (标识、定位任何资源的字符串)
在电脑术语中,统一资源标识符(Uniform Resource Identifier,或URI)是一个用于标识某一互联网资源名称的字符串。 该种标识允许用户对任何(包括本地和互联网)的资源通过特定的协议进行交互操作。URI由包括确定语法和相关协议的方案所定义。
URI的结构: 协议://域名/目录/文件#片段标示符(例如:/a/b.php#a)
根据此协议我们可以自定义协议头 和域名目录等实现不同功能的不同处理
例如: myapp://login?user=admin&pw=123455
接收到这个协议我们就去跳转到登录页面完成自动登录的功能。
实现
Mainifrest配置协议方法
配置完成方法后,当有外部uri或者内部uri访问 scheme和捕捉的一致的时候,会打开InWardActionActivity做相应的处理操作
1.行为的定义
通过枚举的方法将各个协议定义聚合在一起。
2、解析URI
判断是否为 协议头开始,然后取出 action的string 转化为 枚举类型
3、消化处理请求
在电脑术语中,统一资源标识符(Uniform Resource Identifier,或URI)是一个用于标识某一互联网资源名称的字符串。 该种标识允许用户对任何(包括本地和互联网)的资源通过特定的协议进行交互操作。URI由包括确定语法和相关协议的方案所定义。
URI的结构: 协议://域名/目录/文件#片段标示符(例如:/a/b.php#a)
根据此协议我们可以自定义协议头 和域名目录等实现不同功能的不同处理
例如: myapp://login?user=admin&pw=123455
接收到这个协议我们就去跳转到登录页面完成自动登录的功能。
实现
Mainifrest配置协议方法
配置完成方法后,当有外部uri或者内部uri访问 scheme和捕捉的一致的时候,会打开InWardActionActivity做相应的处理操作
<activity android:name=".comm.InWardActionActivity" android:screenOrientation="portrait" > <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:scheme="myapp" /> </intent-filter> </activity>
1.行为的定义
通过枚举的方法将各个协议定义聚合在一起。
/** * 行为分类 * * @author tanping 2015-12-9 详情 action 看客户端文档 */ public static enum ActionType { nearby(1), activity(2), emotion_help(3), video(4), voice(5), person_index(6); final int value; ActionType(int v) { this.value = v; } }
2、解析URI
判断是否为 协议头开始,然后取出 action的string 转化为 枚举类型
/** * 解析action * * @param spec * @return */ public static InwardAction parseAction(String spec) { InwardAction inwardAction = new InwardAction(); VPLog.d("inward", spec + ""); try { if (spec.startsWith("http://") || spec.startsWith("https://")) { inwardAction.mActionType = ActionType.http_web_url; inwardAction.mUri = Uri.parse(spec); return inwardAction; } inwardAction.mUri = Uri.parse(spec); String action = inwardAction.mUri.getAuthority(); inwardAction.mActionType = ActionType.valueOf(action); if (inwardAction.mActionType == null) { return null; } } catch (Exception e) { e.printStackTrace(); return null; } return inwardAction; } ..... /** * 通过key取值 * * @param key * @return */ public String getValueForKey(String key) { if (mUri != null || TextUtils.isEmpty(key)) { return mUri.getQueryParameter(key); } return null; }
3、消化处理请求
/** * start activity的功能根据类型 public 公用 * * @return */ public boolean toStartActivity(Context context) { if (context == null) { context = UIUtils.getContext(); } try { if (mActionType == ActionType.activity) {// 活动 goActivity(context); } else if (mActionType == ActionType.nearby) { goNearActivity(context); } ..... } catch (Exception e) { e.printStackTrace(); } return true; } ...... /** 去执行具体的跳转操作 */ public void goActivity(Context context) { String id = getValueForKey("id"); if (TextUtils.isEmpty(id)) { // Intent intent = new Intent(context, CityActiveActivity.class); Intent intent = new Intent(context, CityActiveListActivity.class); context.startActivity(intent); } else { Intent intent = new Intent(context, ActiveWebActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.putExtra(ActiveWebActivity.KEY_WEB, Integer.valueOf(id)); context.startActivity(intent); } }
相关文章推荐
- Android开发之定义内部类简单实现60s倒计时
- 【精】客户端(iOS、Android)/Server,APP内部的通信协议,跨平台方案
- 【精】客户端(iOS、Android)/Server,APP内部的通信协议,跨平台方案
- Android平台好友点击微信分享的内容后跳转来源App的实现方案研究
- Android Studio第三十八期 - HIOS跳转协议解决URI跳转原生页面并传递参数
- 关于链接跳转APK的技术方案的实现(android)
- Android:scheme 通过uri跳转到APP前后台代码实现
- android 同意协议、服务等,一行文字中掺杂关于其他颜色字体。同时实现超链接点击跳转等功能.
- android屏幕页面实现滚动,页面跳转
- ASP.NET画面跳转实现及其传值问题解决方案
- soap in android HTTP协议实现
- 在web.xml定义,在网页中使用变量实例 使用只有创建变量的servlet才可以访问的<init-param>方式 附带登陆和跳转实现
- Android实现页面跳转的几种方式(转贴)
- Android Binder机制の设计与实现4(Binder 协议)
- android任务管理实现方案+关键代码
- Android模拟 HTTP multipart/form-data 请求协议信息实现图片上传
- Android页面跳转实现方法讲解(纯java)
- Android笔记:实现加载的progressDialog之后跳转到程序主界面
- Android中ContentProvider的实现及定义自己的ContentProvider
- Android 网络电台的一种实现方案