Android匹配Uri工具类UriMatcher

来源:互联网 发布:python编码汉字互转 编辑:程序博客网 时间:2024/06/02 07:09

转载请标明出处: http://blog.csdn.net/wu_wxc/article/details/51386958
本文出自【吴孝城的CSDN博客】

UriMatcher
用来匹配Content provider的URI
urimatcher主要用于匹配URI
官网:http://developer.android.com/intl/zh-cn/reference/android/content/UriMatcher.html

使用

初始化

private static final UriMatcher sURIMatcher = new UriMatcher(UriMatcher.NO_MATCH); 

常量UriMatcher.NO_MATCH表示不匹配任何路径时的返回码
注册需要的URI
“#”为数据的通配符,“*”为所有文本的通配符,匹配码为第三个参数。

static {    sURIMatcher.addURI("cn.wuxiaocheng.people", "name", 1);    sURIMatcher.addURI("cn.wuxiaocheng.people", "age/#", 2);}

与已注册的URI进行匹配

public String getType(Uri uri){    int match = sURIMatcher.match(uri);    switch(match){        case 1:        return "vnd.android.cursor.dir/person";        case 2:        return "vnd.android.cursor.item/person";        default:        return null;    }}

如果.match()方法匹配content://cn.wuxiaocheng.people/name,则返回匹配码为1
如果.match()方法匹配content://cn.wuxiaocheng.people/age/23,则返回匹配码为2

0 0
原创粉丝点击