我已经创建了自己的 ContentProvider,但我不确定如何检索它。调用 getType 方法时,我不断得到一个空值。
这是我的代码:
CurrentPlacesMetaData 类:
public static final String AUTHORITY = "healthcare.management.elderly.providers.CurrentPlacesContentProvider";
public static final String CONTENT_TYPE_PLACES_LIST = "vnd.android.cursor.dir/vnd.healthcare.places";
public static final String CONTENT_TYPE_PLACES_ONE = "vnd.android.cursor.dir/vnd.healthcare.places#";
CurrentPlacesContentProvider 类:
public static final Uri CONTENT_URI = Uri.parse("content://"
+ CurrentPlacesMetaData.AUTHORITY + "/currentplaces");
static {
sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
sUriMatcher.addURI(CurrentPlacesMetaData.AUTHORITY, "places", PLACES_TYPE_LIST);
sUriMatcher.addURI(CurrentPlacesMetaData.AUTHORITY, "places/#", PLACES_TYPE_ONE);
}
@Override
public String getType(Uri uri) {
switch(sUriMatcher.match(uri)) {
case PLACES_TYPE_LIST:
return CurrentPlacesMetaData.CONTENT_TYPE_PLACES_LIST;
case PLACES_TYPE_ONE:
return CurrentPlacesMetaData.CONTENT_TYPE_PLACES_ONE;
default:
throw new IllegalArgumentException("Unknown URI: " + uri);
}
}
尝试调用 ContentProvider 的方法:
Uri uri = Uri.parse("content://healthcare.management.elderly.providers.CurrentPlacesContentProvider/currentplaces/places");
String str = context.getContentResolver().getType(uri)
我为 str 得到一个空值,这可能表明 uri 未被识别。那么我哪里出错了?提前致谢!