首页 > 代码库 > 获取android所有联系人信息

获取android所有联系人信息

转自http://blog.csdn.net/sky181772733/article/details/7221431

这个是代码  

  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, null, null); 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, null, null); 73                 if (emails.moveToFirst()) { 74                     do { 75                         // 遍历所有的电话号码 76                         String emailType = emails 77                                 .getString(emails 78                                         .getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE)); 79                         String emailValue =http://www.mamicode.com/ 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 = IMs100                                 .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_ID112                                         + " = " + contactId, null, null);113                 if (address.moveToFirst()) {114                     do {115                         // 遍历所有的地址116                         String street = address117                                 .getString(address118                                         .getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET));119                         String city = address120                                 .getString(address121                                         .getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY));122                         String region = address123                                 .getString(address124                                         .getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.REGION));125                         String postCode = address126                                 .getString(address127                                         .getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE));128                         String formatAddress = address129                                 .getString(address130                                         .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(organizations150                                 .getColumnIndex(Organization.COMPANY));151                         String title = organizations.getString(organizations152                                 .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(notes168                                 .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(nicknames183                                 .getColumnIndex(Nickname.NAME));184                         Log.i("nickname_", nickname_);185                     } while (nicknames.moveToNext());186                 }187 188             } while (cur.moveToNext());189 190         }191 192     }193 194 }
View Code

权限

    <uses-permission android:name="android.permission.READ_CONTACTS"/>    <uses-permission android:name="android.permission.WRITE_CONTACTS"/>

以上的是获取所有的联系人,在获取的时候会获取 sim卡 和手机本地中的所有的信息,如果在实际操作中要区分出是要求 sim卡中号码 还是 手机本地中的号码 则可以用下面的方法  

 1 首先是手机本地:Cursor cursor = getContentResolver().query(People.CONTENT_URI, null, 2      null, null, null); 3    while (cursor.moveToNext()) { 4     ContactInfo cci = new ContactInfo(); 5     //取得联系人名字 6     int nameFieldColumnIndex = cursor.getColumnIndex(People.NAME); 7     cci.contactName = cursor.getString(nameFieldColumnIndex); 8     //取得电话号码 9     int numberFieldColumnIndex = cursor.getColumnIndex(People.NUMBER);10     cci.userNumber = cursor.getString(numberFieldColumnIndex);11     cci.userNumber = GetNumber(cci.userNumber);12     cci.isChecked = false;13     if (IsUserNumber(cci.userNumber)) {14      if (!IsContain(contactList, cci.userNumber)) {15       if(IsAlreadyCheck(wNumStr, cci.userNumber)){16        cci.isChecked = true;17        numberStr += "," + cci.userNumber;18       }19       contactList.add(cci);20       //Log.i("eoe", "*********"+cci.userNumber);21      }22     }23    }24    cursor.close();25 }
View Code
 1 下面是获取SIM卡: 2 //从SIM卡中取号 3 private void GetSimContact(String add){ 4    //读取SIM卡手机号,有两种可能:content://icc/adn与content://sim/adn 5    try { 6     Intent intent = new Intent(); 7     intent.setData(Uri.parse(add)); 8     Uri uri = intent.getData(); 9     mCursor = getContentResolver().query(uri, null, null, null, null);10     if (mCursor != null) {11      while (mCursor.moveToNext()) {12       ContactInfo sci = new ContactInfo();13       // 取得联系人名字14       int nameFieldColumnIndex = mCursor.getColumnIndex("name");15       sci.contactName = mCursor.getString(nameFieldColumnIndex);16       // 取得电话号码17       int numberFieldColumnIndex = mCursor18         .getColumnIndex("number");19       sci.userNumber = mCursor.getString(numberFieldColumnIndex);20       sci.userNumber = GetNumber(sci.userNumber);21       sci.isChecked = false;22      23       if (IsUserNumber(sci.userNumber)) {24        if (!IsContain(contactList, sci.userNumber)) {25         if(IsAlreadyCheck(wNumStr, sci.userNumber)){26          sci.isChecked = true;27          numberStr += "," + sci.userNumber;28         }29         contactList.add(sci);30         //Log.i("eoe", "*********"+sci.userNumber);31        }32       }33      }34      mCursor.close();35     }36    } catch (Exception e) {37     Log.i("eoe", e.toString());38    }39 }
View Code

 

获取android所有联系人信息