1.Uri
Uri全称Universal Resource Identifier即通用资源标识符,它代表着andorid 中各种操作数据,图像,联系人,信息等都可以用Uri来表示。
Uri一般由三部分组成:
“content://” + 数据的路径 + “标识的Id”
例如:
联系人的Uri: content://contacts/people
某个联系人的Uri: content://contacts/people/1
我们需要解析Uri并从Uri中获取数据,此时android中提供了两个类用于操作uri,分别是UriMatcher 和ContentUris,这两个类对于解析Uri来说十分重要
2.UriMatcher的使用:
UriMatcher用于匹配相应的Uri
首先得初始化:
UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
其次用addUri()为其添加所需要的Uri:
matcher.addURI(AUTHORITY,"sms",SMS_ALL);
matcher.addURI(AUTHORITY,"sms/#",SMS_ONE);
最后进行匹配:
int match = matcher.match(uri);
switch (match){
case SMS_ALL:
break;
case SMS_ONE:
long id = ContentUris.parseId(uri);
selection = "_id = ?";
selectionArgs = new String[]{String.valueOf(id)};
break;
default:
throw new IllegalArgumentException("Wrong Uri :" + uri);
}
match()方法匹配后会返回一个参值,是addUri()时传入的第三个参数。