本文介绍了如何解决多个内容提供商?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了相同的SQLite数据库的两个不同的表的工作,两个内容提供商。他们分享 SQLiteOpenHelper 的单个实例作为的职位描述。每个内容提供商注册在的Andr​​oidManifest.xml 如下:

I created two content providers that work on two different tables of the same SQLite database. They share a single instance of SQLiteOpenHelper as described in the post of Ali Serghini. Each content provider is registered in AndroidManifest.xml as follows.

<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

Each content provider defines the needed content URIs and supplies an 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

When I invoke the content resolver to insert posts, the PostsContentProvider is targeted. When I try to insert comments, however, the content resolver does not refer to the CommentsContentProvider as expected, but calls upon the PostsContentProvider. The result is the following exception which I throw in the PostsContentProvider.

UnsupportedOperationException: Unknown URI: content://com.example.myapp.provider/comments

是否有可能输出目前与内容提供商注册?所有可用的内容的URI

Is it possible to output all available content URIs currently registered with an content provider?

推荐答案

安卓当局必须是唯一的每个内容提供商。该文档是这里从文档引用

The android:authorities needs to be unique for each content provider. The documentation is herequoted from doc

的含量:方案标识为属于一个内容的数据  供应商和授权(com.example.project.healthcareprovider)  识别特定的提供者。因此,当局必须  独一无二的。

这篇关于如何解决多个内容提供商?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 20:26