[英]How to address multiple content providers?
I created two content providers that work on two different tables of the same SQLite database. 我创建了两个内容提供程序,它们在同一个SQLite数据库的两个不同的表上工作。 They share a single instance of SQLiteOpenHelper
as described in the post of Ali Serghini . 他们共享一个SQLiteOpenHelper
实例,如Ali Serghini的帖子所述 。 Each content provider is registered in AndroidManifest.xml
as follows. 每个内容提供程序都在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>
Each content provider defines the needed content URIs and supplies an UriMatcher
. 每个内容提供商定义所需的内容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;
}
When I invoke the content resolver to insert posts, the PostsContentProvider
is targeted. 当我调用内容解析器来插入帖子时, PostsContentProvider
是目标。 When I try to insert comments, however, the content resolver does not refer to the CommentsContentProvider
as expected, but calls upon the PostsContentProvider
. 当我尝试插入注释,但是,内容解析器没有指CommentsContentProvider
如预期,但在来电PostsContentProvider
。 The result is the following exception which I throw in the PostsContentProvider
. 结果是我在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? 是否可以输出当前向内容提供商注册的所有可用内容URI?
The android:authorities
needs to be unique for each content provider. android:authorities
必须对每个内容提供者都是唯一的。 The documentation is here quoted from doc 这里的文档引自doc
The content: scheme identifies the data as belonging to a content provider and the authority (com.example.project.healthcareprovider) identifies the particular provider. 内容:方案将数据标识为属于内容提供者,并且权限(com.example.project.healthcareprovider)标识特定提供者。 The authority therefore must be unique. 因此,权威必须是独一无二的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.