天天看點

使用ContentResolver通路通訊錄ContentProvider

一、概述

ContentProvider是Android四大元件之一,上文以述如果建立一個内容提供者給其他應用去通路,此文将講述用内容解析者去通路系統的通訊錄内容提供者,進行查詢與增加。

二、通訊錄資料庫分析

在通訊錄這個應用裡有個特别重要的三個表,分别為:raw_contacts、data、mimetype。

1、raw_contacts表儲存聯系人的ID 例如:contact_id = 0;

2、data表儲存聯系人的資料,字段data1存放資料,mimetype_id 存放資料類型,raw_contacts_id字段表示儲存資料屬于那個人聯系人的

3、mimetype表儲存資料類型 ,字段mimetype_id = 1 代表email,mimetype_id = 5 代表phone, mimetype_id = 7 代表name

三、編碼思路

1、讀取聯系人資料

1:擷取應用ContentResolver對象

2:建構通路路徑

3:查詢周遊Cursor

public void getContact() {
        ContentResolver contentResolver = getContext().getContentResolver();
        // 擷取raw_contacts表中的contact_id的所有值
        Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");
        // 擷取data表中每一個contact_id對應的data1的值與mimetype的值
        Uri uriData = Uri.parse("content://com.android.contacts/data");
        Cursor cursor = contentResolver.query(uri, null, null, null, null);
        while (cursor.moveToNext()) {

            String id = cursor.getString(cursor.getColumnIndex("contact_id"));
            if (id != null) {
                Cursor cursor1 = contentResolver.query(uriData, null,
                        "raw_contact_id = ?", new String[] {id}, null);
                while (cursor1.moveToNext()) {
                    String str = cursor1.getString(cursor1
                            .getColumnIndex("data1"));
                    String mimetype = cursor1.getString(cursor1
                            .getColumnIndex("mimetype"));
                    Map<String, String> map = new HashMap<>();
                    map.put(mimetype, str);
                    Log.i("ContackTest", map.toString());
                }
                cursor1.close();
            }

        }
        cursor.close();
    }
           

2、向通訊錄寫入資料

1:擷取raw_contacts表中 _id最後一個的值

2:向raw_contacts表中最後插入一行,contact_id = _id + 1;

3:向data表中插入對應的值和mimetype類型,raw_contact_id = contact_id;

public void insertContacts()
    {
           ContentResolver resolver = getContext().getContentResolver();

           Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");
           Uri uri1 = Uri.parse("content://com.android.contacts/data");
           //擷取最後一條的_id的值
           Cursor cursor = resolver.query(uri, new String[]{"_id"}, null, null, null);
           cursor.moveToLast();
           int id = cursor.getInt(cursor.getColumnIndex("_id"));
           cursor.close();
           ContentValues values = new ContentValues();
           int  newId = id + ;
           values.put("contact_id", newId);
           //在最後面插入一條
           resolver.insert(uri, values).toString();
           ContentValues value = new ContentValues();
           //向data表裡插入一條newId的名字的資料
           value.put("data1", "楊威");
           value.put("mimetype", "vnd.android.cursor.item/name");
           value.put("raw_contact_id", newId);
           resolver.insert(uri1, value);
           //向data表裡插入一條newId的電話的資料
           value.put("data1", "17770989700");
           value.put("mimetype", "vnd.android.cursor.item/phone_v2");
           value.put("raw_contact_id", newId);
           resolver.insert(uri1, value);
           //向data表裡插入一條newId的位址的資料
           value.put("data1", "婁底");
           value.put("mimetype", "vnd.android.cursor.item/sip_address");
           value.put("raw_contact_id", newId);
           resolver.insert(uri1, value);

    }
           

注意事項:系統通訊錄的通路授權是:com.android.contacts

表名有:data、raw_contacts

路徑為:content://com.android.contacts/raw_contacts

繼續閱讀