0

我想从 Android 中的联系人详细信息中获取电子邮件地址和电话号码。我可以拿起电话号码,但我希望当它选择电话号码时,它也将电子邮件地址添加到其相应的联系人 ID。所以我可以将它添加到数组列表中。谢谢

public void getContactslist()
     {
         ContentResolver contactResolver = getContentResolver(); 
         Cursor cursor = contactResolver.query(Phone.CONTENT_URI , null,null,null, null);
        // Cursor emails = contactResolver.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,null, null, null); 
         if(cursor.getCount() > 0)
         {
             cursor.moveToFirst();
             do {
                String number = cursor.getString(cursor.getColumnIndex(Phone.NUMBER));
                String displayname = cursor.getString(cursor.getColumnIndex(Phone.DISPLAY_NAME));
                long   contactid   =   cursor.getLong(cursor.getColumnIndex(Phone.CONTACT_ID));
                Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactid, null, null); 
                System.out.println("**emails*******"+number);
                while (emails.moveToNext()) 
                { 
                    String email = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                    Log.e("Email", email);
                }
             }while (cursor.moveToNext() ); 
         }
     }
4

1 回答 1

0

我已经尝试了下面的代码片段并且它有效。希望这对您有所帮助。

public HashMap<String, String> getNameEmailDetails(){
    HashMap<String, String> names = new HashMap<String, String>();
    ContentResolver cr = getContext().getContentResolver();
    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);
                if(email!=null){
                    names.put(name, email);
                }
            }
            cur1.close();
        }
    }
    return names;
}
于 2016-05-09T10:29:40.840 回答