美文网首页
Android ContentProvider

Android ContentProvider

作者: EvanPoison | 来源:发表于2017-05-25 11:05 被阅读10次

前言

内容提供者主要是用来进行应用程序间跨进程数据共享,使用ContentResolver获取数据,使用ContentProvider提供数据

  • Context类中的getContentResolver()获取到实例 ,提供了CRUD操作(通过URI)

  • URI由两部分组成:authority和path 再加上协议

  • 一个标准的内容URI写法:
    content://com.evil.evan.provider/table1
    有了内容uri,我们还需要解析成URI对象,调用URI.parse()

  • 案例1 查询表中的数据
    Cursor cursor = getContentResolver().query(uri, projection, selection, selectionArgs, sortOrder);
    遍历cursor,调用方法cursor.moveToNext()来移动游标

  • 创建内容提供者
    新建一个类去继承ContentProvider,并重写6个抽象方法,CRUD四个,onCreate()和getType()
    匹配uri使用UriMatcher这个类
    getType()返回值由三部分组成:vnd开头+android.cursor.dir/或者android.cursor.item+vnd.<authority>.<path>

相关文章

网友评论

      本文标题:Android ContentProvider

      本文链接:https://www.haomeiwen.com/subject/ycvaxxtx.html