您的位置:首页 > 移动开发 > Android开发

Android 内部跳转URi协议的定义和实现方案

2016-11-24 10:51 267 查看
URI (标识、定位任何资源的字符串)

在电脑术语中,统一资源标识符(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 uri