在android中,我想要所有具有电话号码的联系人详细信息,这样才能获得更好的方法吗?
答案 0 :(得分:4)
这是一个从所有联系人中检索电话号码的实用程序:
public static void getContact(Context ctx) {
//获得所有的联系人
Cursor cur = ctx.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
//循环遍历
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));
if (phoneCount > 0) {
//获得联系人的电话号码
Cursor phones = ctx.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));
Log.d("phoneNumber:",phoneNumber);
} while (phones.moveToNext());
}
}
} while (cur.moveToNext());
}
}
答案 1 :(得分:1)
http://www.tanglei.name/android-get-contacts/
private void getContacts()
{
// Get a cursor with all people
Cursor c = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
null, null,
null);
startManagingCursor(c);
ListAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_2,
c,
new String[] {PhoneLookup.DISPLAY_NAME,PhoneLookup._ID} ,
new int[] {android.R.id.text1,android.R.id.text2});
setListAdapter(adapter);
}