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. }  
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Data;
import android.provider.ContactsContract.CommonDataKinds.Im;
import android.provider.ContactsContract.CommonDataKinds.Nickname;
import android.provider.ContactsContract.CommonDataKinds.Note;
import android.provider.ContactsContract.CommonDataKinds.Organization;
import android.util.Log;

public class ContactActivity extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		// 获得所有的联系人
		Cursor cur = getContentResolver().query(
				ContactsContract.Contacts.CONTENT_URI,
				null,
				null,
				null,
				ContactsContract.Contacts.DISPLAY_NAME
						+ " COLLATE LOCALIZED ASC");
		// 循环遍历
		if (cur.moveToFirst()) {
			int idColumn = cur.getColumnIndex(ContactsContract.Contacts._ID);

			int displayNameColumn = cur
					.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);

			do {
				// 获得联系人的ID号
				String contactId = cur.getString(idColumn);
				// 获得联系人姓名
				String disPlayName = cur.getString(displayNameColumn);
				
				// 查看该联系人有多少个电话号码。如果没有这返回值为0
				int phoneCount = cur
						.getInt(cur
								.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
				Log.i("username", disPlayName);
				if (phoneCount > 0) {
					// 获得联系人的电话号码
					Cursor phones = getContentResolver().query(
							ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
							null,
							ContactsContract.CommonDataKinds.Phone.CONTACT_ID
									+ " = " + contactId, null, null);
					if (phones.moveToFirst()) {
						do {
							// 遍历所有的电话号码
							String phoneNumber = phones
									.getString(phones
											.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
							String phoneType = phones
									.getString(phones
											.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
							Log.i("phoneNumber", phoneNumber);
							Log.i("phoneType", phoneType);
						} while (phones.moveToNext());
					}
				}

				// 获取该联系人邮箱
				Cursor emails = getContentResolver().query(
						ContactsContract.CommonDataKinds.Email.CONTENT_URI,
						null,
						ContactsContract.CommonDataKinds.Phone.CONTACT_ID
								+ " = " + contactId, null, null);
				if (emails.moveToFirst()) {
					do {
						// 遍历所有的电话号码
						String emailType = emails
								.getString(emails
										.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
						String emailValue = emails
								.getString(emails
										.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
						
						Log.i("emailType", emailType);
						Log.i("emailValue", emailValue);
					} while (emails.moveToNext());
				}

				// 获取该联系人IM
				Cursor IMs = getContentResolver().query(
						Data.CONTENT_URI,
						new String[] { Data._ID, Im.PROTOCOL, Im.DATA },
						Data.CONTACT_ID + "=?" + " AND " + Data.MIMETYPE + "='"
								+ Im.CONTENT_ITEM_TYPE + "'",
						new String[] { contactId }, null);
				if (IMs.moveToFirst()) {
					do {
						String protocol = IMs.getString(IMs
								.getColumnIndex(Im.PROTOCOL));
						String date = IMs
								.getString(IMs.getColumnIndex(Im.DATA));
						Log.i("protocol", protocol);
						Log.i("date", date);
					} while (IMs.moveToNext());
				}

				// 获取该联系人地址
				Cursor address = getContentResolver()
						.query(
								ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI,
								null,
								ContactsContract.CommonDataKinds.Phone.CONTACT_ID
										+ " = " + contactId, null, null);
				if (address.moveToFirst()) {
					do {
						// 遍历所有的地址
						String street = address
								.getString(address
										.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET));
						String city = address
								.getString(address
										.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY));
						String region = address
								.getString(address
										.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.REGION));
						String postCode = address
								.getString(address
										.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE));
						String formatAddress = address
								.getString(address
										.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS));
						Log.i("street", street);
						Log.i("city", city);
						Log.i("region", region);
						Log.i("postCode", postCode);
						Log.i("formatAddress", formatAddress);
					} while (address.moveToNext());
				}

				// 获取该联系人组织
				Cursor organizations = getContentResolver().query(
						Data.CONTENT_URI,
						new String[] { Data._ID, Organization.COMPANY,
								Organization.TITLE },
						Data.CONTACT_ID + "=?" + " AND " + Data.MIMETYPE + "='"
								+ Organization.CONTENT_ITEM_TYPE + "'",
						new String[] { contactId }, null);
				if (organizations.moveToFirst()) {
					do {
						String company = organizations.getString(organizations
								.getColumnIndex(Organization.COMPANY));
						String title = organizations.getString(organizations
								.getColumnIndex(Organization.TITLE));
						Log.i("company", company);
						Log.i("title", title);
					} while (organizations.moveToNext());
				}

				// 获取备注信息
				Cursor notes = getContentResolver().query(
						Data.CONTENT_URI,
						new String[] { Data._ID, Note.NOTE },
						Data.CONTACT_ID + "=?" + " AND " + Data.MIMETYPE + "='"
								+ Note.CONTENT_ITEM_TYPE + "'",
						new String[] { contactId }, null);
				if (notes.moveToFirst()) {
					do {
						String noteinfo = notes.getString(notes
								.getColumnIndex(Note.NOTE));
						Log.i("noteinfo", noteinfo);
					} while (notes.moveToNext());
				}

				// 获取nickname信息
				Cursor nicknames = getContentResolver().query(
						Data.CONTENT_URI,
						new String[] { Data._ID, Nickname.NAME },
						Data.CONTACT_ID + "=?" + " AND " + Data.MIMETYPE + "='"
								+ Nickname.CONTENT_ITEM_TYPE + "'",
						new String[] { contactId }, null);
				if (nicknames.moveToFirst()) {
					do {
						String nickname_ = nicknames.getString(nicknames
								.getColumnIndex(Nickname.NAME));
						Log.i("nickname_", nickname_);
					} while (nicknames.moveToNext());
				}

			} while (cur.moveToNext());

		}

	}

}


权限

  1. <uses-permission android:name="android.permission.READ_CONTACTS"/>  
  2. <uses-permission android:name="android.permission.WRITE_CONTACTS"/>  
    <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.      nullnullnull);  
  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. }  
首先是手机本地:Cursor cursor = getContentResolver().query(People.CONTENT_URI, null,
     null, null, null);
   while (cursor.moveToNext()) {
    ContactInfo cci = new ContactInfo();
    //取得联系人名字
    int nameFieldColumnIndex = cursor.getColumnIndex(People.NAME);
    cci.contactName = cursor.getString(nameFieldColumnIndex);
    //取得电话号码
    int numberFieldColumnIndex = cursor.getColumnIndex(People.NUMBER);
    cci.userNumber = cursor.getString(numberFieldColumnIndex);
    cci.userNumber = GetNumber(cci.userNumber);
    cci.isChecked = false;
    if (IsUserNumber(cci.userNumber)) {
     if (!IsContain(contactList, cci.userNumber)) {
      if(IsAlreadyCheck(wNumStr, cci.userNumber)){
       cci.isChecked = true;
       numberStr += "," + cci.userNumber;
      }
      contactList.add(cci);
      //Log.i("eoe", "*********"+cci.userNumber);
     }
    }
   }
   cursor.close();
}


  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, nullnullnullnull);  
  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 = mCursor  
  18.         .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. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值