如何在Android中保存联系人电子邮件?

时间:2011-11-12 09:14:18

标签: java android email contacts

我正在尝试收到已保存的联系人姓名的电子邮件。但是使用我的代码,我只能选择该人的姓名。现在我需要的是,获取所选人员的电子邮件并存储在另一个editText中。怎么实现呢?任何帮助都非常感谢,并提前感谢...我的代码就在这里......

@Override  
protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
    if (resultCode == RESULT_OK) {  
        switch (requestCode) {  
        case PICK_CONTACT:

            final EditText phoneInput = (EditText) findViewById(R.id.person);
            Cursor cursor = null;  
            String phoneNumber = "";
            String Name = "";
            List<String> allNumbers = new ArrayList<String>();
            int phoneIdx = 0;
            try {  
                Uri result = data.getData();  
                String id = result.getLastPathSegment();  
                cursor = getContentResolver().query(Phone.CONTENT_URI, null, Phone.CONTACT_ID + "=?", new String[] { id }, null);  
                phoneIdx = cursor.getColumnIndex(Phone.DISPLAY_NAME);
                if (cursor.moveToFirst()) {
                    while (cursor.isAfterLast() == false) {
                        phoneNumber = cursor.getString(phoneIdx);
                        allNumbers.add(phoneNumber);
                        cursor.moveToNext();
                    }
                } else {
                    //no results actions
                }  
            } catch (Exception e) {  
               //error actions
            } finally {  
                if (cursor != null) {  
                    cursor.close();
                }

1 个答案:

答案 0 :(得分:0)

在我的情况下,2.2 froyo有效,

Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,null,ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId,null, null); 
   while (emails.moveToNext())         
   {         
      emailAddress = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
    }        
   emails.close(); 

有关详细信息,请查看Working With Android Contacts