17

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>

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;
    }

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

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

JoxTraex
  • 13,423
  • 6
  • 32
  • 45
JJD
  • 50,076
  • 60
  • 203
  • 339
  • 2
    "I created two content providers that work on two different tables of the same SQLite database" -- why not use one `ContentProvider` that works "on two different tables of the same SQLite database"? That's what the path is for in the `Uri`, to allow you to distinguish between different tables or the like. – CommonsWare Aug 15 '12 at 21:09
  • @CommonsWare Because I plan to use a `SyncAdapter` in the future and I heard that a provider's data can only be synced if it is a separate provider. Please correct me if I am wrong. I am happy to read any documentation on that topic. – JJD Aug 15 '12 at 21:19
  • Ah, OK. I haven't played with `SyncAdapter` yet. I would be somewhat surprised by the limitation you cite, but I have no knowledge of whether that limitation exists or not. Sorry! – CommonsWare Aug 15 '12 at 21:22
  • No offense taken. The `SyncAdapter` is a mysterious thing still to me. I researched quite a while but I am still not sure whether I can [use a `SyncAdapter` to sync "normal" data with a REST backend](http://stackoverflow.com/questions/11906172/synchronize-android-client-and-rest-server). – JJD Aug 15 '12 at 22:02

1 Answers1

33

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

The content: scheme identifies the data as belonging to a content provider and the authority (com.example.project.healthcareprovider) identifies the particular provider. The authority therefore must be unique.

nandeesh
  • 24,740
  • 6
  • 69
  • 79
  • 2
    I saw this one coming by the time I found this post, but thank you for confirming. Strangely enough, the documentation currently does not mention anymore that the name should be unique? – Martijn de Milliano Feb 19 '13 at 18:26
  • Ten years later, confirming that the docs still don't mention that the authorities should be unique. However, it does say (in an example) "The authority `com.example.project.healthcareprovider` identifies the provider itself," which taken at face value, suggests that the authority has to be unique, in order to uniquely identify the provider. – LarsH May 05 '23 at 15:52