如何从特定联系人处获取电子邮件ID

时间:2013-08-07 06:34:39

标签: android android-contacts

我有以下代码来获取联系人的电子邮件地址它从联系人获取所有电子邮件,但我想特定的联系电子邮件ID 我在stackoverflow中提到了一些答案,但不满足。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.update_contact);
    update_contacts = (Button) findViewById(R.id.button1);
    update_name = (EditText) findViewById(R.id.update_name);
    update_phonenumber = (EditText) findViewById(R.id.update_phonenumber);
    Bundle extras = getIntent().getExtras();
    get_name = extras.getString("contact_name");
    update_name.setText(get_name);


    ContentResolver cr = getContentResolver();

/ *获取显示名称* /

的联系电话
         Cursor pCur = cr.query(
        ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
        null, 
        ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME +" = ?", 
        new String[]{get_name}, null);
         if(pCur.getCount() > 0){
            while (pCur.moveToNext()) {
                id = pCur.getString(
                        pCur.getColumnIndex(ContactsContract.Contacts._ID));
                String number = pCur.getString(
                          pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                update_phonenumber.setText(number);

            }
         }
        pCur.close();

 /* get the email id for the display name */ 

        Cursor emailCur = cr.query( 
                ContactsContract.CommonDataKinds.Email.CONTENT_URI, 
                null,
                ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", 
                new String[]{String.valueOf(id)}, null);

            if(emailCur.getCount() > 0){

                while (emailCur.moveToNext()) { 
                    String email = emailCur.getString(
                                  emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                    String emailType = emailCur.getString(
                                  emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE)); 
                    Toast.makeText(getApplicationContext(), ""+email, Toast.LENGTH_LONG).show();
                } 
            }
            emailCur.close(); }

1 个答案:

答案 0 :(得分:2)

您可以比较get_name和name字段,例如以下方式可以获取电子邮件ID。

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

                Cursor emailCur = getContentResolver().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));
                        if(get_name.equals(name)) {
                            Log.e("Email id",email);
                        }
                   } 
                    emailCur.close();
                }

            }