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

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);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值