通讯录是Android手机自带的一个应用,它是一个ContentProvider应用,其它应用可以对通讯录进行访问,进行对联系人的CRUD操作。
首先,我们可以在File Explorer视图下找到contacts2.db文件,这是通讯录的文件
然后,我们用SQLite打开,分析下它的数据库结构:
这三张表的结构介绍完了,接下来我们说下它们之间的联系:
raw_contacts表存放联系人的记录ID:字段名称是_id
data表存放联系人的信息:_id是主键,raw_contacts_id对应raw_contacts表的_id,mimetype_id字段对应的是mimetypes表的_id
mimetypes表存放data表的每条记录的属性:_id是主键,为1的时候是email类型
public void testContacts() throws Exception{ Uri uri = Uri.parse("content://com.android.contacts/contacts"); //获得一个ContentResolver数据共享的对象 ContentResolver reslover = getContext().getContentResolver(); //取得联系人中开始的游标,通过content://com.android.contacts/contacts这个路径获得 Cursor cursor = reslover.query(uri, null, null, null, null); //上边的所有代码可以由这句话代替:Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); //Uri.parse("content://com.android.contacts/contacts") == ContactsContract.Contacts.CONTENT_URI while(cursor.moveToNext()){ //获得联系人ID String id = cursor.getString(cursor.getColumnIndex(android.provider.ContactsContract.Contacts._ID)); //获得联系人姓名 String name = cursor.getString(cursor.getColumnIndex(android.provider.ContactsContract.Contacts.DISPLAY_NAME)); //获得联系人手机号码 Cursor phone = reslover.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + id, null, null); StringBuilder sb = new StringBuilder("contactid=").append(id).append(name); while(phone.moveToNext()){ //取得电话号码(可能存在多个号码) int phoneFieldColumnIndex = phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER); String phoneNumber = phone.getString(phoneFieldColumnIndex); sb.append(phoneNumber+"www"); } //建立一个Log,使得可以在LogCat视图查看结果 Log.i(TAG, sb.toString()); } }
//根据号码获取联系人的姓名 public void testContactNameByNumber() throws Exception{ String number = "110"; Uri uri = Uri.parse("content://com.android.contacts/data/phones/filter/"+number); ContentResolver resolver = getContext().getContentResolver(); Cursor cursor = resolver.query(uri, new String[]{android.provider.ContactsContract.Data.DISPLAY_NAME}, null, null, null); if(cursor.moveToFirst()){ String name = cursor.getString(0); Log.i(TAG, name); } cursor.close(); }
//添加联系人 public void testAddContact() throws Exception{ Uri uri = Uri.parse("content://com.android.contacts/raw_contacts"); ContentResolver resolver = getContext().getContentResolver(); ContentValues values = new ContentValues(); long contactid = ContentUris.parseId(resolver.insert(uri, values)); uri = Uri.parse("content://com.android.contacts/data"); //添加姓名 values.put("raw_contact_id", contactid); values.put(Data.MIMETYPE, "vnd.android.cursor.item/name"); values.put("data1", "xiaoming"); resolver.insert(uri, values); values.clear(); //添加电话 values.put("raw_contact_id", contactid); values.put(Data.MIMETYPE, "vnd.android.cursor.item/phone_v2"); values.put("data1", "1234120155"); resolver.insert(uri, values); values.clear(); //添加Email values.put("raw_contact_id", contactid); values.put(Data.MIMETYPE, "vnd.android.cursor.item/email_v2"); values.put("data1", "1234120155@qq.com"); resolver.insert(uri, values); }
//批量添加 public void testAddContact2() throws Exception{ Uri uri = Uri.parse("content://com.android.contacts/raw_contacts"); ContentResolver resolver = getContext().getContentResolver(); ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>(); ContentProviderOperation op1 = ContentProviderOperation.newInsert(uri) .withValue("account_name", null) .build(); operations.add(op1); uri = Uri.parse("content://com.android.contacts/data"); //添加姓名 ContentProviderOperation op2 = ContentProviderOperation.newInsert(uri) .withValueBackReference("raw_contact_id", 0) .withValue("mimetype", "vnd.android.cursor.item/name") .withValue("data2", "李小龙") .build(); operations.add(op2); //添加电话号码 ContentProviderOperation op3 = ContentProviderOperation.newInsert(uri) .withValueBackReference("raw_contact_id", 0) .withValue("mimetype", "vnd.android.cursor.item/phone_v2") .withValue("data1", "1234120155") .withValue("data2", "2") .build(); operations.add(op3); resolver.applyBatch("com.android.contacts", operations); }
public void testDelete()throws Exception{ String name = "李小龙"; //根据姓名求id Uri uri = Uri.parse("content://com.android.contacts/raw_contacts"); ContentResolver resolver = this.getContext().getContentResolver(); Cursor cursor = resolver.query(uri, new String[]{Data._ID},"display_name=?", new String[]{name}, null); if(cursor.moveToFirst()){ int id = cursor.getInt(0); //根据id删除data中的相应数据 resolver.delete(uri, "display_name=?", new String[]{name}); uri = Uri.parse("content://com.android.contacts/data"); resolver.delete(uri, "raw_contact_id=?", new String[]{id+""}); } }
Android开发系列(十一):对手机通讯录的读取、添加、删除、查找
原文地址:http://blog.csdn.net/u010800530/article/details/39455347