Android系统中的联系人也是通过ContentProvider来对外提供数据的,我们这里实现获取所有联系人、通过电话号码获取联系人、添加联系人、使用事务添加联系人。
获取所有联系人
1. Android系统中的联系人也是通过ContentProvider来对外提供数据的
2. 数据库路径为:/data/data/com.android.providers.contacts/database/contacts2.db
3. 我们需要关注的有3张表
raw_contacts:其中保存了联系人id
data:和raw_contacts是多对一的关系,保存了联系人的各项数据
mimetypes:为数据类型
4. Provider的authorites为com.android.contacts
5. 查询raw_contacts表的路径为:contacts
6. 查询data表的路径为:contacts/#/data
这个路径为连接查询,要查询“mimetype”字段可以根据“mimetype_id”查询到mimetypes表中的数据
7. 先查询raw_contacts得到每个联系人的id,在使用id从data表中查询对应数据,根据mimetype分类数据
示例:
[html]
view plain
copy
print
?
- //查询所有联系人
- public void testGetAll() {
- ContentResolver resolver = getContext().getContentResolver();
- Uri uri = Uri.parse("content://com.android.contacts/contacts");
- Cursor idCursor = resolver.query(uri, new String[] { "_id" }, null, null, null);
- while (idCursor.moveToNext()) {
- //获取到raw_contacts表中的id
- int id = idCursor.getInt(0);
- //根据获取到的ID查询data表中的数据
- uri = Uri.parse("content://com.android.contacts/contacts/" + id + "/data");
- Cursor dataCursor = resolver.query(uri, new String[] { "data1", "mimetype" }, null, null, null);
- StringBuilder sb = new StringBuilder();
- sb.append("id=" + id);
- //查询联系人表中的
- while (dataCursor.moveToNext()) {
- String data = dataCursor.getString(0);
- String type = dataCursor.getString(1);
- if ("vnd.android.cursor.item/name".equals(type))
- sb.append(", name=" + data);
- else if ("vnd.android.cursor.item/phone_v2".equals(type))
- sb.append(", phone=" + data);
- else if ("vnd.android.cursor.item/email_v2".equals(type))
- sb.append(", email=" + data);
- }
- System.out.println(sb);
- }
- }
//查询所有联系人 public void testGetAll() { ContentResolver resolver = getContext().getContentResolver(); Uri uri = Uri.parse("content://com.android.contacts/contacts"); Cursor idCursor = resolver.query(uri, new String[] { "_id" }, null, null, null); while (idCursor.moveToNext()) { //获取到raw_contacts表中的id int id = idCursor.getInt(0); //根据获取到的ID查询data表中的数据 uri = Uri.parse("content://com.android.contacts/contacts/" + id + "/data"); Cursor dataCursor = resolver.query(uri, new String[] { "data1", "mimetype" }, null, null, null); StringBuilder sb = new StringBuilder(); sb.append("id=" + id); //查询联系人表中的 while (dataCursor.moveToNext()) { String data = dataCursor.getString(0); String type = dataCursor.getString(1); if ("vnd.android.cursor.item/name".equals(type)) sb.append(", name=" + data); else if ("vnd.android.cursor.item/phone_v2".equals(type)) sb.append(", phone=" + data); else if ("vnd.android.cursor.item/email_v2".equals(type)) sb.append(", email=" + data); } System.out.println(sb); } }
通过电话号码获取联系人
1. 系统内部提供了根据电话号码获取data表数据的功能,路径为:data/phones/filter/*
2. 用电话号码替换“*”部分就可以查到所需数据,获取“display_name”可以获取到联系人显示名
示例:
[html]
view plain
copy
print
?
- //根据电话号码查询联系人名称
- public void testGetName() {
- ContentResolver resolver = getContext().getContentResolver();
- Uri uri = Uri.parse("content://com.android.contacts/data/phones/filter/1111");
- Cursor c = resolver.query(uri, new String[] { "display_name" }, null, null, null);
- while (c.moveToNext()) {
- System.out.println(c.getString(0));
- }
- }
//根据电话号码查询联系人名称 public void testGetName() { ContentResolver resolver = getContext().getContentResolver(); Uri uri = Uri.parse("content://com.android.contacts/data/phones/filter/1111"); Cursor c = resolver.query(uri, new String[] { "display_name" }, null, null, null); while (c.moveToNext()) { System.out.println(c.getString(0)); } }
添加联系人
1. 先向raw_contacts表插入id,路径为:raw_contacts
2. 得到id之后再向data表插入数据,路径为:data
示例:
[html]
view plain
copy
print
?
- //添加联系人
- ublic void testInsert() {
- ContentResolver resolver = getContext().getContentResolver();
- Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");
- ContentValues values = new ContentValues();
- // 向raw_contacts插入一条除了ID之外, 其他全部为NULL的记录, ID是自动生成的
- long id = ContentUris.parseId(resolver.insert(uri, values));
- //添加联系人姓名
- uri = Uri.parse("content://com.android.contacts/data");
- values.put("raw_contact_id", id);
- values.put("data2", "FHM");
- values.put("mimetype", "vnd.android.cursor.item/name");
- resolver.insert(uri, values);
- //添加联系人电话
- values.clear(); // 清空上次的数据
- values.put("raw_contact_id", id);
- values.put("data1", "18600000000");
- values.put("data2", "2");
- values.put("mimetype", "vnd.android.cursor.item/phone_v2");
- resolver.insert(uri, values);
- //添加联系人邮箱
- values.clear();
- values.put("raw_contact_id", id);
- values.put("data1", "[email protected]");
- values.put("data2", "1");
- values.put("mimetype", "vnd.android.cursor.item/email_v2");
- resolver.insert(uri, values);
//添加联系人 public void testInsert() { ContentResolver resolver = getContext().getContentResolver(); Uri uri = Uri.parse("content://com.android.contacts/raw_contacts"); ContentValues values = new ContentValues(); // 向raw_contacts插入一条除了ID之外, 其他全部为NULL的记录, ID是自动生成的 long id = ContentUris.parseId(resolver.insert(uri, values)); //添加联系人姓名 uri = Uri.parse("content://com.android.contacts/data"); values.put("raw_contact_id", id); values.put("data2", "FHM"); values.put("mimetype", "vnd.android.cursor.item/name"); resolver.insert(uri, values); //添加联系人电话 values.clear(); // 清空上次的数据 values.put("raw_contact_id", id); values.put("data1", "18600000000"); values.put("data2", "2"); values.put("mimetype", "vnd.android.cursor.item/phone_v2"); resolver.insert(uri, values); //添加联系人邮箱 values.clear(); values.put("raw_contact_id", id); values.put("data1", "[email protected]"); values.put("data2", "1"); values.put("mimetype", "vnd.android.cursor.item/email_v2"); resolver.insert(uri, values); }
使用事务添加联系人
1. 在添加联系人得时候是分多次访问Provider,如果在过程中出现异常,会出现数据不完整的情况,这些操作应该放在一次事务中
2. 使用ContentResolver的applyBatch(String authority,ArrayList<ContentProviderOperation> operations) 方法可以将多个操作在一个事务中执行
3. 文档位置:
file:///F:/android-sdk-windows/docs/reference/android/provider/ContactsContract.RawContacts.html
示例:
[html]
view plain
copy
print
?
- //使用事务添加联系人
- public void testInsertBatch() throws Exception {
- ContentResolver resolver = getContext().getContentResolver();
- ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>();
- ContentProviderOperation operation1 = ContentProviderOperation //
- .newInsert(Uri.parse("content://com.android.contacts/raw_contacts")) //
- .withValue("_id", null) //
- .build();
- operations.add(operation1);
- ContentProviderOperation operation2 = ContentProviderOperation //
- .newInsert(Uri.parse("content://com.android.contacts/data")) //
- .withValueBackReference("raw_contact_id", 0) //
- .withValue("data2", "ZZH") //
- .withValue("mimetype", "vnd.android.cursor.item/name") //
- .build();
- operations.add(operation2);
- ContentProviderOperation operation3 = ContentProviderOperation //
- .newInsert(Uri.parse("content://com.android.contacts/data")) //
- .withValueBackReference("raw_contact_id", 0) //
- .withValue("data1", "18612312312") //
- .withValue("data2", "2") //
- .withValue("mimetype", "vnd.android.cursor.item/phone_v2") //
- .build();
- operations.add(operation3);
- ContentProviderOperation operation4 = ContentProviderOperation //
- .newInsert(Uri.parse("content://com.android.contacts/data")) //
- .withValueBackReference("raw_contact_id", 0) //
- .withValue("data1", "[email protected]") //
- .withValue("data2", "2") //
- .withValue("mimetype", "vnd.android.cursor.item/email_v2") //
- .build();
- operations.add(operation4);
- // 在事务中对多个操作批量执行
- resolver.applyBatch("com.android.contacts", operations);
- }
//使用事务添加联系人 public void testInsertBatch() throws Exception { ContentResolver resolver = getContext().getContentResolver(); ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>(); ContentProviderOperation operation1 = ContentProviderOperation // .newInsert(Uri.parse("content://com.android.contacts/raw_contacts")) // .withValue("_id", null) // .build(); operations.add(operation1); ContentProviderOperation operation2 = ContentProviderOperation // .newInsert(Uri.parse("content://com.android.contacts/data")) // .withValueBackReference("raw_contact_id", 0) // .withValue("data2", "ZZH") // .withValue("mimetype", "vnd.android.cursor.item/name") // .build(); operations.add(operation2); ContentProviderOperation operation3 = ContentProviderOperation // .newInsert(Uri.parse("content://com.android.contacts/data")) // .withValueBackReference("raw_contact_id", 0) // .withValue("data1", "18612312312") // .withValue("data2", "2") // .withValue("mimetype", "vnd.android.cursor.item/phone_v2") // .build(); operations.add(operation3); ContentProviderOperation operation4 = ContentProviderOperation // .newInsert(Uri.parse("content://com.android.contacts/data")) // .withValueBackReference("raw_contact_id", 0) // .withValue("data1", "[email protected]") // .withValue("data2", "2") // .withValue("mimetype", "vnd.android.cursor.item/email_v2") // .build(); operations.add(operation4); // 在事务中对多个操作批量执行 resolver.applyBatch("com.android.contacts", operations); }