获取android联系人信息

  1. import  android.app.Activity;  
  2. import  android.database.Cursor;  
  3. import  android.os.Bundle;  
  4. import  android.provider.ContactsContract;  
  5. import  android.provider.ContactsContract.Data;  
  6. import  android.provider.ContactsContract.CommonDataKinds.Im;  
  7. import  android.provider.ContactsContract.CommonDataKinds.Nickname;  
  8. import  android.provider.ContactsContract.CommonDataKinds.Note;  
  9. import  android.provider.ContactsContract.CommonDataKinds.Organization;  
  10. import  android.util.Log;  
  11.   
  12. public   class  ContactActivity  extends  Activity {  
  13.     /** Called when the activity is first created. */   
  14.     @Override   
  15.     public   void  onCreate(Bundle savedInstanceState) {  
  16.         super .onCreate(savedInstanceState);  
  17.         setContentView(R.layout.main);  
  18.   
  19.         // 获得所有的联系人   
  20.         Cursor cur = getContentResolver().query(  
  21.                 ContactsContract.Contacts.CONTENT_URI,  
  22.                 null ,  
  23.                 null ,  
  24.                 null ,  
  25.                 ContactsContract.Contacts.DISPLAY_NAME  
  26.                         + " COLLATE LOCALIZED ASC" );  
  27.         // 循环遍历   
  28.         if  (cur.moveToFirst()) {  
  29.             int  idColumn = cur.getColumnIndex(ContactsContract.Contacts._ID);  
  30.   
  31.             int  displayNameColumn = cur  
  32.                     .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);  
  33.   
  34.             do  {  
  35.                 // 获得联系人的ID号   
  36.                 String contactId = cur.getString(idColumn);  
  37.                 // 获得联系人姓名   
  38.                 String disPlayName = cur.getString(displayNameColumn);  
  39.                   
  40.                 // 查看该联系人有多少个电话号码。如果没有这返回值为0   
  41.                 int  phoneCount = cur  
  42.                         .getInt(cur  
  43.                                 .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));  
  44.                 Log.i("username" , disPlayName);  
  45.                 if  (phoneCount >  0 ) {  
  46.                     // 获得联系人的电话号码   
  47.                     Cursor phones = getContentResolver().query(  
  48.                             ContactsContract.CommonDataKinds.Phone.CONTENT_URI,  
  49.                             null ,  
  50.                             ContactsContract.CommonDataKinds.Phone.CONTACT_ID  
  51.                                     + " = "  + contactId,  nullnull );  
  52.                     if  (phones.moveToFirst()) {  
  53.                         do  {  
  54.                             // 遍历所有的电话号码   
  55.                             String phoneNumber = phones  
  56.                                     .getString(phones  
  57.                                             .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));  
  58.                             String phoneType = phones  
  59.                                     .getString(phones  
  60.                                             .getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));  
  61.                             Log.i("phoneNumber" , phoneNumber);  
  62.                             Log.i("phoneType" , phoneType);  
  63.                         } while  (phones.moveToNext());  
  64.                     }  
  65.                 }  
  66.   
  67.                 // 获取该联系人邮箱   
  68.                 Cursor emails = getContentResolver().query(  
  69.                         ContactsContract.CommonDataKinds.Email.CONTENT_URI,  
  70.                         null ,  
  71.                         ContactsContract.CommonDataKinds.Phone.CONTACT_ID  
  72.                                 + " = "  + contactId,  nullnull );  
  73.                 if  (emails.moveToFirst()) {  
  74.                     do  {  
  75.                         // 遍历所有的电话号码   
  76.                         String emailType = emails  
  77.                                 .getString(emails  
  78.                                         .getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));  
  79.                         String emailValue = emails  
  80.                                 .getString(emails  
  81.                                         .getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));  
  82.                           
  83.                         Log.i("emailType" , emailType);  
  84.                         Log.i("emailValue" , emailValue);  
  85.                     } while  (emails.moveToNext());  
  86.                 }  
  87.   
  88.                 // 获取该联系人IM   
  89.                 Cursor IMs = getContentResolver().query(  
  90.                         Data.CONTENT_URI,  
  91.                         new  String[] { Data._ID, Im.PROTOCOL, Im.DATA },  
  92.                         Data.CONTACT_ID + "=?"  +  " AND "  + Data.MIMETYPE +  "='"   
  93.                                 + Im.CONTENT_ITEM_TYPE + "'" ,  
  94.                         new  String[] { contactId },  null );  
  95.                 if  (IMs.moveToFirst()) {  
  96.                     do  {  
  97.                         String protocol = IMs.getString(IMs  
  98.                                 .getColumnIndex(Im.PROTOCOL));  
  99.                         String date = IMs  
  100.                                 .getString(IMs.getColumnIndex(Im.DATA));  
  101.                         Log.i("protocol" , protocol);  
  102.                         Log.i("date" , date);  
  103.                     } while  (IMs.moveToNext());  
  104.                 }  
  105.   
  106.                 // 获取该联系人地址   
  107.                 Cursor address = getContentResolver()  
  108.                         .query(  
  109.                                 ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI,  
  110.                                 null ,  
  111.                                 ContactsContract.CommonDataKinds.Phone.CONTACT_ID  
  112.                                         + " = "  + contactId,  nullnull );  
  113.                 if  (address.moveToFirst()) {  
  114.                     do  {  
  115.                         // 遍历所有的地址   
  116.                         String street = address  
  117.                                 .getString(address  
  118.                                         .getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET));  
  119.                         String city = address  
  120.                                 .getString(address  
  121.                                         .getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY));  
  122.                         String region = address  
  123.                                 .getString(address  
  124.                                         .getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.REGION));  
  125.                         String postCode = address  
  126.                                 .getString(address  
  127.                                         .getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE));  
  128.                         String formatAddress = address  
  129.                                 .getString(address  
  130.                                         .getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS));  
  131.                         Log.i("street" , street);  
  132.                         Log.i("city" , city);  
  133.                         Log.i("region" , region);  
  134.                         Log.i("postCode" , postCode);  
  135.                         Log.i("formatAddress" , formatAddress);  
  136.                     } while  (address.moveToNext());  
  137.                 }  
  138.   
  139.                 // 获取该联系人组织   
  140.                 Cursor organizations = getContentResolver().query(  
  141.                         Data.CONTENT_URI,  
  142.                         new  String[] { Data._ID, Organization.COMPANY,  
  143.                                 Organization.TITLE },  
  144.                         Data.CONTACT_ID + "=?"  +  " AND "  + Data.MIMETYPE +  "='"   
  145.                                 + Organization.CONTENT_ITEM_TYPE + "'" ,  
  146.                         new  String[] { contactId },  null );  
  147.                 if  (organizations.moveToFirst()) {  
  148.                     do  {  
  149.                         String company = organizations.getString(organizations  
  150.                                 .getColumnIndex(Organization.COMPANY));  
  151.                         String title = organizations.getString(organizations  
  152.                                 .getColumnIndex(Organization.TITLE));  
  153.                         Log.i("company" , company);  
  154.                         Log.i("title" , title);  
  155.                     } while  (organizations.moveToNext());  
  156.                 }  
  157.   
  158.                 // 获取备注信息   
  159.                 Cursor notes = getContentResolver().query(  
  160.                         Data.CONTENT_URI,  
  161.                         new  String[] { Data._ID, Note.NOTE },  
  162.                         Data.CONTACT_ID + "=?"  +  " AND "  + Data.MIMETYPE +  "='"   
  163.                                 + Note.CONTENT_ITEM_TYPE + "'" ,  
  164.                         new  String[] { contactId },  null );  
  165.                 if  (notes.moveToFirst()) {  
  166.                     do  {  
  167.                         String noteinfo = notes.getString(notes  
  168.                                 .getColumnIndex(Note.NOTE));  
  169.                         Log.i("noteinfo" , noteinfo);  
  170.                     } while  (notes.moveToNext());  
  171.                 }  
  172.   
  173.                 // 获取nickname信息   
  174.                 Cursor nicknames = getContentResolver().query(  
  175.                         Data.CONTENT_URI,  
  176.                         new  String[] { Data._ID, Nickname.NAME },  
  177.                         Data.CONTACT_ID + "=?"  +  " AND "  + Data.MIMETYPE +  "='"   
  178.                                 + Nickname.CONTENT_ITEM_TYPE + "'" ,  
  179.                         new  String[] { contactId },  null );  
  180.                 if  (nicknames.moveToFirst()) {  
  181.                     do  {  
  182.                         String nickname_ = nicknames.getString(nicknames  
  183.                                 .getColumnIndex(Nickname.NAME));  
  184.                         Log.i("nickname_" , nickname_);  
  185.                     } while  (nicknames.moveToNext());  
  186.                 }  
  187.   
  188.             } while  (cur.moveToNext());  
  189.   
  190.         }  
  191.   
  192.     }  
  193.   
  194. }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值