---
title: "通过ContactsContract类获取电话号码的改变"
author: "huangro"
date: "2010-09-02T15:19:55Z"
tags: []
categories: ["移动开发"]
slug: "contactscontract"
draft: false
lastmod: "2010-09-02T15:40:08Z"
id: 753588
url: "https://huangro.iteye.com/blog/753588"
---

在2.0版本之前,获取通讯录中的联系人及其电话号码的方式如下:

```java

        String string = "";
        super.onCreate(savedInstanceState);
        ContentResolver cr = getContentResolver();
        Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
        while (cursor.moveToNext()) 
        {
        	int nameFieldColumnIndex = cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME);
        	String contact = cursor.getString(nameFieldColumnIndex);
        	int numberFieldColumnIndex = cursor.getColumnIndex(PhoneLookup.NUMBER);
        	String number = cursor.getString(numberFieldColumnIndex);
        	string += (contact+":"+number+"\n");
        }

```

但是在2.0及之后的版本中,获取联系人的方式不变,但是电话号码的方式发生改变, 具体代码如下:

```java

        String string = "";
        super.onCreate(savedInstanceState);
        ContentResolver cr = getContentResolver();
        Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
        while (cursor.moveToNext()) 
        {
        	int nameFieldColumnIndex = cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME);
        	String contact = cursor.getString(nameFieldColumnIndex);
        	string += contact + ":";
        	String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
        	Cursor phone = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, 
        			ContactsContract.CommonDataKinds.Phone.CONTACT_ID+"="+contactId, null, null);
        	while (phone.moveToNext()) 
        	{
        		int numberFieldColumnIndex = phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
        		String strPhoneNumber = phone.getString(numberFieldColumnIndex);
        		string += " " + strPhoneNumber;
        	}
        	string += "\n";
        	phone.close();
        }
        cursor.close();

```


可见,在2.0之后的版本中要获取手机号码就只能通过先取得联系人ID然后再取得联系人电话号码, 而且号码可能不止一个.