当前位置: 代码迷 >> Android >> android2.0contacts的一些步骤
  详细解决方案

android2.0contacts的一些步骤

热度:101   发布时间:2016-05-01 15:35:40.0
android2.0contacts的一些方法

依次浏览你的电话本联系人

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)) { ?
? ? ? ? ? ? ? ?
// You know have the number so now query it like this?
? ?
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()) { ?
? ? ? ? ? ? ? ?
// This would allow you get several email addresses ?
? ?
String emailAddress = emails.getString( ?
? ? ? emails
.getColumnIndex( ?
? ? ? ?
ContactsContract.CommonDataKinds.CommonDataColumns.DATA)); ?
? ?
} ?
? ?emails
.close(); ?
?
} ?
? cursor
.close();?

?


得到电话本中的姓名和号码

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);?
?
int numberFieldColumnIndex = people.getColumnIndex(PhoneLookup.NUMBER);?
? ? ?
String number = people.getString(nameFieldColumnIndex);?
}?
?
people
.close();?

如果你想得到一些 notes?

private String getNote(long contactId) { ?
?
String note = null; ?
?
String[] columns = new String[] ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
{ ContactsContract.CommonDataKinds.Note.NOTE }; ?
?
String where = ContactsContract.Data.RAW_CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?"; ?
?
String[] whereParameters = new String[]{Long.toString(contactId), ?
? ?
ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE}; ?
?
Cursor contacts = getContentResolver().query ?
?
(ContactsContract.Data.CONTENT_URI, projection, where, ?
? ? whereParameters
, null); ?
?
if (contacts.moveToFirst()) { ?
? ?rv
= contacts.getString(0); ?
?
} ?
? contacts
.close(); ?
?
return note; ?
?
}?