16

我创建了两个内容提供程序,它们在同一个 SQLite 数据库的两个不同表上工作。它们共享一个实例,SQLiteOpenHelperAli 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?

4

1 回答 1

31

每个内容提供商的android:authorities需求必须是唯一的。该文档在此处 引用自 doc

content: 方案将数据标识为属于内容提供者,并且权限 (com.example.project.healthcareprovider) 标识特定提供者。因此,权限必须是唯一的。

于 2012-08-15T21:14:38.830 回答