zoukankan      html  css  js  c++  java
  • Android中的内容提供器

    用途

    不同于File, SharedPreferences和DataBase,Content Provider主要用于不同的应用程序间共享数据,允许一个程序安全的访问另一个程序中的数据。

    用法

    通过Context的getContentResolver()取得该类的实例。然后是和数据库相似的CRUD操作,其中query()方法返回Cursor对象,可以通过moveToNext()将数据逐一读出,然后是cursor.clse()关闭。

            // Get the Cursor of all the contacts
            Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI,
                    null, null, null, null);
    
            // Move the cursor to first. Also check whether the cursor is empty or not.
            if (cursor.moveToFirst()) {
                // Iterate through the cursor
                do {
                    // Get the contacts name
                    String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                    contacts.add(name);
                } while (cursor.moveToNext());
            }
            // Close the curosor
            cursor.close();

    自定义内容提供器

    如果要实现自己的内容提供器,要继承自ContentProvider,然后去覆盖onCreate, query, insert, update, delete, getType六个方法,这六个方法必须全部重写。

  • 相关阅读:
    Git

    学而不记则徒劳无功
    Redis基础
    哈希表
    第一个Python程序
    Python 环境搭建 基于 Windows
    执行数据库的插入操作 insert
    Eclipse连接到My sql数据库的操作总结/配置数据库驱动
    数据库 (一)
  • 原文地址:https://www.cnblogs.com/dracohan/p/5980844.html
Copyright © 2011-2022 走看看