android 2.0读取联系人

一下文章引自网络快照

http://203.208.37.132/search?q=cache:65WdqffxH-UJ:konshi.blogspot.com/2009/12/android-20.html+ContactsContract&cd=16&hl=zh-CN&ct=clnk&gl=cn&inlang=zh-CN&client=aff-avalanche&st_usg=ALhdy2-Kg5_1uawrMLIyiSfUHm1PLeA1GQ

如果使用了
import
android.provider.Contacts;
import android.provider.Contacts.People;
就要特別注意一下,
因為在android 2.0 SDK當中不建議使用,
請改用
import android.provider.ContactsContract;
import android.provider.ContactsContract.PhoneLookup;

要取得聯絡人的姓名sample code如下:
final Cursor people = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);

while(people.moveToNext())
{
int nameFieldColumnIndex = people.getColumnIndex(PhoneLookup.DISPLAY_NAME);
String contact = people.getString(nameFieldColumnIndex);
}
people.close();
奇怪的事,如果要取得聯絡人的電話
如果用
int nameFieldColumnIndex = people.getColumnIndex(PhoneLookup.NUMBER);
String phone = people.getString(nameFieldColumnIndex);

nameFieldColumnIndex return回來的值總是 ─1,

上網google了一下,找到瞭解法,

1. 首先找到目標聯絡人的 _ID,例如要找第一個聯絡人的_ID
Cursor target = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
target.moveToFirst();
String contactId = cursor.getString(
target.getColumnIndex(ContactsContract.Contacts._ID));

2. 接著透過_ID,來找電話號碼,電話號碼可能不只有一個,所以先判斷有無電話號碼

String IsPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

3. 如果有電話,接著再去query電話號碼

if( (Integer.parseInt(
IsPhone) > 0) )
{
Cursor phoneNumber = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null);
while (phones.moveToNext())
{
String strPhoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

}
}

** 如果要查詢e-mail,程式如下
Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,null,ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId,null, null);

while (emails.moveToNext())
{
String emailAddress = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));

}

另外

Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);   
while (cursor.moveToNext()) 
{    
	String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
    String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
    if (Boolean.parseBoolean(hasPhone)) 
	{
		Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null);     
		while (phones.moveToNext()) 
		{      
			String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));	
		}     
		phones.close();    
	}    
	Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,null,ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId,null, null);
    while (emails.moveToNext()) 
	{                 
		String emailAddress = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.CommonDataColumns.DATA));
	}    
	emails.close();   
}   
cursor.close();

 

你可能感兴趣的:(android)