简体   繁体   English

如何在Android中获取已保存的联系人电子邮件?

[英]How to get saved contact email in android?

I am trying to get the email of the saved contact name. 我正在尝试获取已保存的联系人姓名的电子邮件。 but with my code, i could only select the name of the person. 但是使用我的代码,我只能选择此人的名字。 Now what i need is, get the email of the selected person and store in another editText. 现在,我需要的是获取所选人员的电子邮件,并将其存储在另一个editText中。 How to achieve it? 如何实现呢? Any help is really appreciated and thanks in advance... My code goes here... 非常感谢您的帮助,在此先感谢...我的代码在这里...

@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();
                }

In my case for 2.2 froyo this works, 以我的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(); 

For more info please look at Working With Android Contacts 有关更多信息,请参阅使用Android联系人

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM