我创建了两个内容提供程序,它们在同一个SQLite数据库的两个不同的表上工作。他们将SQLiteOpenHelper
的单个实例共享为described in the post of 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?
答案 0 :(得分:27)
android:authorities
对每个内容提供商都必须是唯一的。文档是here
引自doc
内容:方案将数据标识为属于内容 提供者和权威(com.example.project.healthcareprovider) 标识特定提供者。因此必须是权威 唯一的。