zoukankan      html  css  js  c++  java
  • android笔记 : Content provider内容提供器

    内容提供器(Content Provider)主要用于在不同的应用程序之间实现数据共享的功能.

    内容提供器的用法一般有两种,一种是使用现有的内容提供器来读取和操作相应程序中
    的数据,另一种是创建自己的内容提供器给我们程序的数据提供外部访问接口。

    第一种方法:使用ContentResolver来读取和操作相应程序中的数据

    1.用parse方法将内容 URI 字符串解析成 Uri 对象.

    uri(Uniform Resource identifier)指的是统一资源标识符
    Uri uri = Uri .parse("content://com.example.databasetest.provider/book");

    2.进行增删改查的操作,如下所示:

    query(查):与SqliteDatabase类似,需要借助cursor .

    Cursor cursor = getContentResolver().query(uri,projection,selection,selectionArgs,sortOrder);

    if (cursor != null) {
    while (cursor.moveToNext()) {
    String column1 = cursor.getString(cursor.getColumnIndex("column1"));
    int column2 = cursor.getInt(cursor.getColumnIndex("column2"));
    }
    cursor.close();
    }

    insert(增):实例化ContentValues ,调用put方法添加键值对,然后调用 getContentResolver的insert方法.

    ContentValues values = new ContentValues();
    values.put("column1", "text");
    values.put("column2", 1);
    getContentResolver().insert(uri, values);

    update(改):实例化ContentValues ,调用put方法添加键值对,然后调用 getContentResolver的update方法.

    ContentValues values = new ContentValues();
    values.put("name", "A Storm of Swords");
    values.put("pages", 1216);
    values.put("price", 24.05);
    getContentResolver().update(uri, values, null, null);

    delete(删):调用 ContentResolver delete()方法将数据删除

    getContentResolver().delete(uri, null, null);

    第二种方法:创建自己的内容提供器

    a. 创建一个继承了ContentProvider父类的类,重写onCreate(),query(),update(),insert(),delete(),getType()方法

    b. 定义一个名为CONTENT_URI,并且是public static final的Uri类型的类变量,你必须为其指定一个唯一的字符串值,最好的方案是以类的全名称, 如:
    public static final Uri CONTENT_URI = Uri.parse( “content://com.google.android.MyContentProvider”);

    c. 定义你要返回给客户端的数据列名。如果你正在使用Android数据库,必须为其定义一个叫_id的列,它用来表示每条记录的唯一性。

    d. 创建你的数据存储系统。大多数Content Provider使用Android文件系统或SQLite数据库来保持数据,但是你也可以以任何你想要的方式来存储。

    e. 如果你要存储字节型数据,比如位图文件等,数据列其实是一个表示实际保存文件的URI字符串,通过它来读取对应的文件数据。处理这种数据类型的 Content Provider需要实现一个名为_data的字段,_data字段列出了该文件在Android文件系统上的精确路径。这个字段不仅是供客户端使用,而 且也可以供ContentResolver使用。客户端可以调用ContentResolver.openOutputStream()方法来处理该 URI指向的文件资源;如果是ContentResolver本身的话,由于其持有的权限比客户端要高,所以它能直接访问该数据文件。

    f. 声明public static String型的变量,用于指定要从游标处返回的数据列。

    g. 查询返回一个Cursor类型的对象。所有执行写操作的方法如insert(), update() 以及delete()都将被监听。我们可以通过使用ContentResover().notifyChange()方法来通知监听器关于数据更新的信息。

    h. 在AndroidMenifest.xml中使用<provider>标签来设置Content Provider。

    i. 如果你要处理的数据类型是一种比较新的类型,你就必须先定义一个新的MIME类型,以供ContentProvider.geType(url)来返回。MIME类型有两种形式:一种是为指定的单个记录的,还有一种是为多条记录的。这里给出一种常用的格式:

      vnd.android.cursor.item/vnd.yourcompanyname.contenttype (单个记录的MIME类型)
      比如, 一个请求列车信息的URI如content://com.example.transportationprovider/trains/122 可能就会返回typevnd.android.cursor.item/vnd.example.rail这样一个MIME类型。

      vnd.android.cursor.dir/vnd.yourcompanyname.contenttype (多个记录的MIME类型)
      比如, 一个请求所有列车信息的URI如content://com.example.transportationprovider/trains 可能就会返回vnd.android.cursor.dir/vnd.example.rail这样一个MIME 类型。

    参考资料:

    1.《第一行代码》内容提供器;

    2.CSDN博客:http://blog.csdn.net/chuyuqing/article/details/39995607

  • 相关阅读:
    ZOJ 1002 Fire Net (火力网)
    UVa OJ 117 The Postal Worker Rings Once (让邮差只走一圈)
    UVa OJ 118 Mutant Flatworld Explorers (变体扁平世界探索器)
    UVa OJ 103 Stacking Boxes (嵌套盒子)
    UVa OJ 110 MetaLoopless Sorts (无循环元排序)
    第一次遇到使用NSNull的场景
    NSURL使用浅析
    从CNTV下载《小小智慧树》
    NSDictionary and NSMutableDictionary
    Category in static library
  • 原文地址:https://www.cnblogs.com/expiator/p/5747625.html
Copyright © 2011-2022 走看看