2

我有以下代码从联系人那里获取电子邮件地址,它从联系人那里获取所有电子邮件 ID,但我想要特定的联系人电子邮件 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(); }
4

1 回答 1

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

            }
于 2013-08-07T12:24:15.847 回答