标签:
通讯录是Android手机自带的一个应用,它是一个ContentProvider应用,其它应用可以对通讯录进行访问,进行对联系人的CRUD操作。
首先,我们可以在File Explorer视图下找到contacts2.db文件,这是通讯录的文件
然后,我们用SQLite打开,分析下它的数据库结构:
这三张表的结构介绍完了,接下来我们说下它们之间的联系:<喎?"http://www.2cto.com/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+cmF3X2NvbnRhY3Rzse205rfFwarPtcjLtcS8x8K8SUSjutfWts7D+7PGysdfaWQ8L3A+CjxwPmRhdGGx7bTmt8XBqs+1yMu1xNDFz6Kjul9pZMrH1ve8/KOscmF3X2NvbnRhY3RzX2lkttTTpnJhd19jb250YWN0c7HttcRfaWSjrG1pbWV0eXBlX2lk19a2zrbU06a1xMrHbWltZXR5cGVzse21xF9pZDwvcD4KPHA+bWltZXR5cGVzse205rfFZGF0YbHttcTDv8z1vMfCvLXEyvTQ1KO6X2lkysfW97z8o6zOqjG1xMqxuvLKx2VtYWlswODQzTwvcD4KPHA+PGJyPgo8L3A+CjxoMT7I/aGiz9TKvqGizO2806Giyb6z/aGisunV0sGqz7XIy6O6PC9oMT4KPGgyPjGhorvxyKHL+dPQtcTBqs+1yMujujwvaDI+CjxwPjwvcD4KPHByZSBjbGFzcz0="brush:java;">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()); } }
1
2
3
4
5
6
7
8
9
10
11
12
|
//根据号码获取联系人的姓名 public void testContactNameByNumber() throws Exception{ String number = "110" ; 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(); } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
//添加联系人 public void testAddContact() throws Exception{ ContentResolver resolver = getContext().getContentResolver(); ContentValues values = new ContentValues(); long contactid = ContentUris.parseId(resolver.insert(uri, values)); //添加姓名 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); } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
//批量添加 public void testAddContact2() throws Exception{ ContentResolver resolver = getContext().getContentResolver(); ArrayList<contentprovideroperation> operations = new ArrayList<contentprovideroperation>(); ContentProviderOperation op1 = ContentProviderOperation.newInsert(uri) .withValue( "account_name" , null ) .build(); operations.add(op1); //添加姓名 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); }</contentprovideroperation></contentprovideroperation> |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
public void testDelete() throws Exception{ String name = "李小龙" ; //根据姓名求id 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}); resolver.delete(uri, "raw_contact_id=?" , new String[]{id+ "" }); } } |
Android开发系列(十一):对手机通讯录的读取、添加、删除、查找
标签:
原文地址:http://www.cnblogs.com/yanganok/p/4584043.html