gpt4 book ai didi

java - 从Android联系人列表中获取用户信息

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:10:27 24 4
gpt4 key购买 nike

我需要从我的联系人列表中获取每个联系人的姓名、电子邮件和电话号码。

问题是我可以得到姓名和电话号码,但不能得到电子邮件。我得到的是电话号码而不是电子邮件。

这是我的代码:

Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,null, null);
while (phones.moveToNext()) {

String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String email = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Email.ADDRESS));

ContactItem objContact = new ContactItem();
objContact.setName(name);
objContact.setPhoneNo(phoneNumber);
objContact.setEmail(email);
list.add(objContact);

}
phones.close();

最佳答案

    Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
while (cursor.moveToNext()) {
String contactId = cursor.getString(cursor.getColumnIndex(
ContactsContract.Contacts._ID));

//May want to get basic info here like name, phone
//Example:
//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));
// Log.i("phone", phoneNumber);
//}
//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.Email.DATA));

Log.i("emails", emailAddress);
}
emails.close();
}
cursor.close();

Source and Reference: How to read contacts on Android 2.0

关于java - 从Android联系人列表中获取用户信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17420799/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com