我创建了两个内容提供程序,它们在同一个 SQLite 数据库的两个不同表上工作。它们共享一个实例,SQLiteOpenHelper
如Ali Serghini 的帖子中所述。每个内容提供者注册AndroidManifest.xml
如下。
<provider
android:name=".contentprovider.PostsContentProvider"
android:authorities="com.example.myapp.provider"
android:exported="false"
android:multiprocess="true" >
</provider>
<provider
android:name=".contentprovider.CommentsContentProvider"
android:authorities="com.example.myapp.provider"
android:exported="false"
android:multiprocess="true" >
</provider>
每个内容提供者定义所需的内容 URI 并提供一个UriMatcher
.
public class PostsProvider extends BaseContentProvider {
private static final UriMatcher sUriMatcher = buildUriMatcher();
private static final int POSTS = 100;
private static final int POST_ID = 101;
private static UriMatcher buildUriMatcher() {
final UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
final String authority = CustomContract.CONTENT_AUTHORITY;
matcher.addURI(authority, DatabaseProperties.TABLE_NAME_POSTS, POSTS);
matcher.addURI(authority, DatabaseProperties.TABLE_NAME_POSTS + "/#", POST_ID);
return matcher;
}
...
public class CommentsProvider extends BaseContentProvider {
protected static final UriMatcher sUriMatcher = buildUriMatcher();
protected static final int COMMENTS = 200;
protected static final int COMMENT_ID = 201;
private static UriMatcher buildUriMatcher() {
final UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
final String authority = CustomContract.CONTENT_AUTHORITY;
matcher.addURI(authority, DatabaseProperties.TABLE_NAME_COMMENTS, COMMENTS);
matcher.addURI(authority, DatabaseProperties.TABLE_NAME_COMMENTS + "/#", COMMENT_ID);
return matcher;
}
当我调用内容解析器来插入帖子时,这PostsContentProvider
是有针对性的。但是,当我尝试插入评论时,内容解析器并未按预期引用CommentsContentProvider
,而是调用PostsContentProvider
. 结果是我在PostsContentProvider
.
UnsupportedOperationException: Unknown URI: content://com.example.myapp.provider/comments
是否可以输出当前向内容提供者注册的所有可用内容 URI?