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?
| 归档时间: |
|
| 查看次数: |
9757 次 |
| 最近记录: |