• android-ContentProvider


    一、理解ContentProvider

      为了在应用程序之间交换数据,Android提供了ContentProvider,它是不同应用程序之间进行数据交换的标准API,当一个应用程序需要把自己的数据暴露给其他应用程序时,该应用就可以通过ContentProvider实现;其它应用程序就可以通过ContentResolver来操作ContentProvider暴露的数据。一旦某个应用程序通过ContentProvider暴露了自己的数据操作接口,那么不管该应用程序是否启动,其它应用程序都可以该接口来操作该应用程序内部的数据; ContentProvider也是Android应用四大组件之一,需要在AndroidManifest.xml文件中进行配置。

      ContentProvider以某种Uri的形式对外提供数据,允许其他应用访问或修改数据;其他应用程序使用ContentResolver根据Uri去访问操作指定数据据;也就是说Uri是ContentProvider和ContentResolver进行数据交换的标识。

       1、 Uri可分成如下三个部分:

           content://org.crazyit.providers.dictprovider/words

          content://   --- 类似上网协议默认是http://,暴露ContentProvider、访问ContentProvider的协议默认是content://;

          org.crazyit.providers.dictprovider --- ContentProvider的authorities,系统根据这个部分来找到操作哪个ContentProvider;

             words    --- 数据部分,当访问者需要访问不同资源时,这个部分是动态改变的。

       2、 被支持的Uri形式:

          content://org.crazyit.providers.dictprovider/word/2  (访问word数据中ID为2的记录)

          content://org.crazyit.providers.dictprovider/word/2/word  (访问word数据中ID为2的记录的word字段)

          content://org.crazyit.providers.dictprovider/words  (访问全部数据)

       3、   大部分使用ContentProvider所操作的数据来自数据库,有时候也可来自文件、XML、网络等其它存储方式,此时Uri也可以改为如下形式:

            content://org.crazyit.providers.dictprovider/word/detail/  (操作word节点下的detail节点)

        4、字符串转换成Uri

          Uri工具类提供了静态方法parse()可将一个字符串转换成Uri

    二、ContentProvider简介

      1、开发ContentProvider步骤:

        (1)定义自己的ContentProvider类,该类需要继承Android提供的ContentProvider基类;

        (2)在AndroidManifest.xml 文件中注册该ContentProvider,注册时需要为其绑定一个Uri;

        (3)提供如下六个方法:

          1>public boolean onCreate() 

            该方法在ContentProvider创建后会被调用,其他应用程序第一次访问ContentProvider时,该ContentProvider会被创建出来,并立即回调onCreate()方法。

          2>public Uri insert(Uri uri,ContentValues values)

            跟据Uri插入values对应的数据。

          3>public int delete(Uri uri,String selection,String[] selectionArgs)

            跟据Uri删除selection条件所匹配的全部记录。

          4>public int update(Uri uri,ContentValues values,String selection,String[] selectionArgs)

            根据Uri修改selection条件所匹配全部记录。

          5>public Cursor query(Uri uri,String[] projection,String selection,String[] selectionArgs,String sortOrder)

            根据Uri查询出selection条件所匹配的全部记录,projection指定列名。

          6>public String getType(Uri uri)

            该方法用于返回当前Uri代表的数据的MIME类型,如果该Uri对应的数据可能包括多条记录,那么MIME类型字符串应该以 vnd.android.cursor.dir/ 开头,如果该Uri对应的数据只包含一条记录,那么MIME类型字符串应该以 vnd.android.cursor.item/ 开头。

    三、使用ContentResolver操作数据

      1、应用程序提供ContentResolver来操作ContentProvider所暴露的数据;

      2、Context提供了getContentResolver()来获取ContentResolver对象,获取ContentResolver对象后就可以调用ContentResolver的如下方法来操作其他应用程序的数据了:

        (1)insert(Uri uri,ContentValues values) 向Uri的ContentProvider中插入values对应的数据;

        (2)delete(Uri uri,String where,String[] selectionArgs) 删除Uri对应的ContentProvider中where提交的数据;

        (3)update(Uri uri,ContentValues values,String where,String[] selectionArgs) 更新Uri对应的ContentProvider中where提交匹配的数据;

        (4)query(Uri uri,String[] projection,String selection,String[] selectionArgs,String sortOrder) 查询Uri对应的ContentProvider中where提交匹配的数据。

  • 相关阅读:
    实现一个简单的ConnectionPool
    并发连接MySQL
    C#里面滥用String造成的性能问题
    String.IndexOf
    C#代码中插入X86汇编
    正确理解Handle对象
    orleans发送广播消息
    log日志方法
    PHP 批量插入数据
    逻辑漏洞小结之SRC篇
  • 原文地址:https://www.cnblogs.com/iamkk/p/5933478.html
Copyright © 2020-2023  润新知