得到联系人列表中的电子邮件地址 [英] get Email Address from contact list

查看:123
本文介绍了得到联系人列表中的电子邮件地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到的联系人列表通过

I getting contact list by

许可

android:name="android.permission.READ_CONTACTS"


Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);

但如何从获得电子邮件地址

but how to get Email address from

 public void onActivityResult(int reqCode, int resultCode, Intent data) {
//what should i have to write to fetch email address of selected contact
// I wrote like below but i could not get result

 if (resultCode == Activity.RESULT_OK) {  
     try{
             Uri contactData = data.getData();

             Cursor cursorEmail = getContentResolver().query(contactData,null,null,null,null);
             cursorEmail.moveToFirst();
             String emailAdd =  cursorEmail.getString(cursorEmail.getColumnIndex(ContactsContract.CommonDataKinds.Email.ADDRESS));
             Toast.makeText(MySettings.this, emailAdd, Toast.LENGTH_LONG).show();
          }catch(Exception e){
              Toast.makeText(MySettings.this, "No Email Add found", Toast.LENGTH_LONG).show();
          }

}

但问题是,我没有得到的电子邮件地址从选定的联系人列表,以便任何一个可以给我解决方案

but the problem is that i am not getting Email address from selected contact list so can any one give me solution

推荐答案

您可以使用下面的code检索电子邮件。

You can use following code to retrieve email.

public ArrayList<String> ShowContact() {        

    nameList = new ArrayList<String>();
            phoneList = new ArrayList<String>();
            emailList = new ArrayList<String>();

    ContentResolver cr = 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));
            String name = cur
                    .getString(cur
                            .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            if (Integer
                    .parseInt(cur.getString(cur
                            .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                // Query phone here. Covered next

                Cursor pCur = cr.query(
                        ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                        null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                + " = ?", new String[] { id }, null);
                while (pCur.moveToNext()) {
                    // Do something with phones
                    String phoneNo = pCur
                            .getString(pCur
                                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

                    nameList.add(name); // Here you can list of contact.
                        phoneList.add(phoneNo); // Here you will get list of phone number.                  


                    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.DATA));                            

                 emailList.add(email); // Here you will get list of email    

                        } 
                        emailCur.close();       
                }
                pCur.close();
            }
        }
    }

    return nameList; // here you can return whatever you want.
}
}

这篇关于得到联系人列表中的电子邮件地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆