如何解决多个内容提供商?

JJD*_*JJD 16 android uri android-contentresolver android-contentprovider

我创建了两个内容提供程序,它们在同一个SQLite数据库的两个不同的表上工作.它们共享的单个实例SQLiteOpenHelper作为阿里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>
Run Code Online (Sandbox Code Playgroud)

每个内容提供商定义所需的内容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;
    }
Run Code Online (Sandbox Code Playgroud)

...

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;
    }
Run Code Online (Sandbox Code Playgroud)

当我调用内容解析器来插入帖子时,它PostsContentProvider是有针对性的.当我尝试插入注释,但是,内容解析器并没有CommentsContentProvider符合市场预期,但在该调用PostsContentProvider.结果是我抛出的以下异常PostsContentProvider.

UnsupportedOperationException: Unknown URI: content://com.example.myapp.provider/comments
Run Code Online (Sandbox Code Playgroud)

是否可以输出当前向内容提供商注册的所有可用内容URI?

nan*_*esh 28

android:authorities需要为每个内容提供商的独特.这里的文档 引自doc

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

  • 当我发现这篇文章时,我看到了这个,但感谢您的确认。奇怪的是,目前的文档没有再提到名称应该是唯一的? (2认同)