Android-查询联系人和增加联系人

系统表data/com.android.providers.contacts/databases/contacts2.db存放着联系人的信息

表:

  a) 表raw_contacts

     重要字段:_id  display_name

  b) 表data 

   重要字段: minitype_id  raw_contact_id data1 data2 ....

  c) 表minitypes

   重要字段:mimetype

查看源码:

https://github.com/android
https://github.com/android/platform_packages_providers_contactsprovider

  platform_packages_providers_contactsprovider

一、查询联系人

  a) TestContacts.java

 1 public class TestContacts extends AndroidTestCase {
 2 
 3     public void getAllContactsInfo()throws Exception{
 4         
 5         String rawbase = "content://com.android.contacts/raw_contacts";
 6         String database = "content://com.android.contacts/data";
 7         
 8         Uri uri = Uri.parse(rawbase);
 9         Cursor cursor = getContext().getContentResolver().query(uri, null, null, null, null);
10         
11         while(cursor.moveToNext()){
12             
13             String id = cursor.getString(cursor.getColumnIndex("_id"));//取得raw_contacts中的字段_id
14             //System.out.println("_id = "+id);
15             Cursor datacursor = getContext().getContentResolver().query(Uri.parse(database), null, "raw_contact_id=?", new String[]{id}, null);
16             //查询表data中通过字段raw_contact_id = _id(raw_contacts中的)
17             
18             while(datacursor.moveToNext()){
19                 
20                 /*String[] names = datacursor.getColumnNames();
21                 for (String str : names) {
22                     System.out.println(str);
23                 }*/
24                 
25                 String mimetype = datacursor.getString(datacursor.getColumnIndex("mimetype"));
26                 //取得表mimetypes表中字段mimetype , 表data中通过data1和mimietype_id和表mimetypes的_id进行了多表查询
27                 System.out.println("mimetype = "+mimetype);
28                 if("vnd.android.cursor.item/name".equals(mimetype)){
29                     String data1 =  datacursor.getString(datacursor.getColumnIndex("data1"));//取得data表中字段data1
30                     System.out.println("姓名: "+data1);
31                 }else if("vnd.android.cursor.item/phone_v2".equals(mimetype)){
32                     String data1 =  datacursor.getString(datacursor.getColumnIndex("data1"));//取得data表中字段data1
33                     System.out.println("电话: "+data1);
34                 }else if("vnd.android.cursor.item/postal-address_v2".equals(mimetype)){
35                     String data1 =  datacursor.getString(datacursor.getColumnIndex("data1"));//取得data表中字段data1
36                     System.out.println("地址: "+data1);
37                 }else if("vnd.android.cursor.item/email_v2".equals(mimetype)){
38                     String data1 =  datacursor.getString(datacursor.getColumnIndex("data1"));//取得data表中字段data1
39                     System.out.println("邮箱: "+data1);
40                 }
41             }
42             datacursor.close();
43         }
44         cursor.close();
45     }
46 }

  b)AndroidManifest.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 3     package="cn.zengfansheng.contact"
 4     android:versionCode="1"
 5     android:versionName="1.0" >
 6 
 7     <uses-sdk android:minSdkVersion="8" />
 8     
 9      <instrumentation
10         android:name="android.test.InstrumentationTestRunner"
11         android:targetPackage="cn.zengfansheng.contact" 
12         android:label="Test for My App Contact"
13         />
14      
15      <uses-permission android:name="android.permission.READ_CONTACTS"/>
16      <uses-permission android:name="android.permission.WRITE_CONTACTS"/>
17      
18     <application
19         android:icon="@drawable/ic_launcher"
20         android:label="@string/app_name" >
21         <uses-library android:name="android.test.runner"/>
22     </application>
23 
24 </manifest>

二、添加联系人

 1 //    增加联系人
 2     /*
 3      * 应用:
 4      *  qq 同步助手
 5          号簿管家 -> 文件 -> 上传服务器 
 6          pim 90多字段  体力活 
 7      */
 8     public void writeContact() throws Exception{
 9         
10         String rawbase = "content://com.android.contacts/raw_contacts";
11         Uri url = Uri.parse(rawbase);
12         ContentValues values = new ContentValues();
13         Uri uri = getContext().getContentResolver().insert(url , values );
14         //返回的raw_contact 表中的_id
15         long id = ContentUris.parseId(uri);
16         
17         String database = "content://com.android.contacts/data";
18         
19         ContentValues nameValues = new ContentValues();
20         nameValues.put("mimetype", "vnd.android.cursor.item/phone_v2");
21         nameValues.put("data1", "hacket");
22         nameValues.put("raw_contact_id", id);
23         getContext().getContentResolver().insert(Uri.parse(database), nameValues);
24         
25         ContentValues phoneValues = new ContentValues();
26          phoneValues.put("mimetype", "vnd.android.cursor.item/phone_v2");
27          phoneValues.put("data1", "18974677184");
28          phoneValues.put("raw_contact_id", id);
29          getContext().getContentResolver().insert(Uri.parse(database), phoneValues);
30          
31          ContentValues emailValues = new ContentValues();
32          emailValues.put("mimetype", "vnd.android.cursor.item/email_v2");
33          emailValues.put("data1", "zeng_fansheng@sina.cn");
34          emailValues.put("raw_contact_id", id);
35          getContext().getContentResolver().insert(Uri.parse(database), emailValues);
36 
37     }

 

 需要增加

加入读写联系人信息的权限
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
 
posted @ 2013-06-09 10:22  hacket520  阅读(419)  评论(0编辑  收藏  举报