1. 程式人生 > >FC 7.3 Content Provider之簡單使用

FC 7.3 Content Provider之簡單使用

  • 簡介
  • ContentResolver的使用
  • 查詢、增加、修改、刪除
  • 應用:介紹上一篇獲取通訊錄部分的程式碼
  • 自定義內容提供器

ContentProvider可以讓不同應用程式之間進行資料共享,它還可以選擇只對哪一部分資料進行共享,從而保證程式中的隱私資料不會有洩漏風險。所以元件ContentProvider主要負責儲存和共享資料。

ContentProvider有兩種形式:可以使用現有的內容提供者來讀取和操作相應程式中的資料,也可以建立自己的內容提供者給這個程式的資料提供外部訪問介面。(ContentProvider有對外共享資料的功能,換句話說,其他應用程式可以通過ContentProvider對應用中的資料進行增刪改查SQLite資料儲存的時候就提到過可以實現增刪改查的各種輔助性方法,實際上ContentProvider是對SQLiteOpenHelper的進一步封裝,因此它們使用的方法太像了,只不過不再用單純的表名

指明被操作的表,畢竟現在是其他程式訪問它,而是用有一定格式規範的內容URI來代替。)

 

ContentResolver的基本用法,方法需要Uri引數,所以先介紹一下URI的組成。

content://com.example.app.provider/table

 URI 可以非常清楚地表達出我們想要訪問哪個程式中哪張表裡的資料,但還沒完,還需要將它解析成 Uri 物件才可以作為引數傳入。通過呼叫 Uri.parse()方法,就可以將內容 URI 字串解析成 Uri 物件了,程式碼如下:

Uri rui=Uri.parse("content://com.example.app.provider/table")

然後就可以使用這個Uri物件來查詢table表的資料了

Cursor cursor=getContentResolver().query(
    uri,
    projection,
    selection,
    selectionArgs,
    sortOrder);
方法引數 對應sql 描述
uri from table_name 某個應用程式下的一張表
projection select column1 查詢指定的列名
selection where column =value 指定where的約束條件
selectionArgs   為where中的佔位符提供具體的值
sortOrder order by column1 指定查詢結果的排序方式

查詢完後返回cursor物件,我們可以將資料從cursor物件中讀取出來。

if (cursor != null) {
    while (cursor.moveToNext()) {
    // 獲取聯絡人姓名
    String displayName = cursor.getString(cursor.getColumnIndex("column1"));
    // 獲取聯絡人手機號
    String number = cursor.getString(cursor.getColumnIndex("column2"));
    }    
}

查詢資料後,接下來進行增加

ContentValues values = new ContentValues();
values.put("column1", 1);
values.put("column2", 2);
getContentResolver().insert(Uri, values); // 插入第一條資料

更新:

ContentValues values = new ContentValues();
values.put("column1", 11);
getContentResolver().update(Uri, values, "column1= ?", new String[] { "6" });

刪除:

getContentResolver().delete("Uri", "column = ?", new String[] { "500" });

上一篇文章介紹的是獲取通訊錄

介紹其中readContacts方法

  • getContentResolver().query()來查詢系統的聯絡人資料
  • ContactsContract.CommonDataKinds.Phone.CONTENT_URI對應的就是uri,其他的引數都傳null
  • 接著對cursor進行遍歷,取出資料,新增到listview
  • 最終將cursor關閉
private void readContacts() {
        Cursor cursor = null;
        try {
            // 查詢聯絡人資料
            cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
            if (cursor != null) {
                while (cursor.moveToNext()) {
                    // 獲取聯絡人姓名
                    String displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                    // 獲取聯絡人手機號
                    String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    contactsList.add(displayName + "\n" + number);
                }
                adapter.notifyDataSetChanged();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }

內容提供器及自定義內容提供器,這裡有篇很好的文章訪問自定義內容提供器,大佬的文章寫的就是好,自己要向大佬學習。。。