我是Android的新手,我希望每次使用以下代码时都会在我的联系人列表中收到现有联系人的电子邮件我无法获得确切的联系电子邮件,但随机电子邮件。 请帮我纠正这段代码,我将非常感谢。
public void fetchingContacts(Context context) {
Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
ContentResolver cr = context.getContentResolver();
Cursor crsr = cr.query(uri, null, null, null, null);
crsr.moveToFirst();
if (crsr.getCount() > 0) {
while (crsr.moveToNext()) {
ID = crsr
.getString(crsr
.getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID));
id = crsr.getString(crsr.getColumnIndex(BaseColumns._ID));
NAME = crsr
.getString(crsr
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
NUMBER = crsr
.getString(crsr
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
arylist_contact_id.add(ID);
arylist_all_number.add(NUMBER);
arylist_all_names.add(NAME);
// ////////////
Cursor emailCur = cr.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null, ContactsContract.CommonDataKinds.Email.CONTACT_ID
+ " = " + id, null, null);
while (emailCur.moveToNext()) {
email = emailCur
.getString(emailCur
.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
Log.v("fetchingEMAIL --- ", email);
}
emailCur.close();
Log.v("IN do while :: name and number ", ID + " , id :" + id
+ " " + NAME + " : " + NUMBER + " : " + email);
}
}
crsr.close();
}
答案 0 :(得分:1)
Cursor emailCur = cr.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID
+ " = ?", new String[] { id }, null);
while (emailCur.moveToNext()) {
String email = emailCur
.getString(emailCur
.getColumnIndex(ContactsContract.CommonDataKinds.Email.ADDRESS));
}
emailCur.close();
以上是有助于正确获取电子邮件的查询我的朋友对我有用...希望尝试一下。
答案 1 :(得分:0)
试试这个
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
Cursor cur1 = cr.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
new String[]{id}, null);
while (cur1.moveToNext()) {
//to get the contact names
String name=cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
Log.e("Name :", name);
String email = cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
Log.e("Email", email);
}
}
cur1.close();
}