无法从联系人中检索电子邮件

时间:2013-06-03 21:39:28

标签: android android-intent android-contentprovider android-contacts

我正在编写一个代码来获取所选联系人的电子邮件,但是当我选择联系人并返回主要活动时,我无法收到电子邮件(未显示吐司)...

public static final int PICK_CONTACT = 1;

public void onclick(View view) {
    Intent intent1 = new Intent(Intent.ACTION_PICK,
            ContactsContract.Contacts.CONTENT_URI);
    startActivityForResult(intent1,PICK_CONTACT);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode==PICK_CONTACT) {
        try {
            if(resultCode==Activity.RESULT_OK) {
                Uri uri=data.getData();
                String[] projection = new String[] {
                    ContactsContract.Contacts._ID,
                    ContactsContract.Contacts.DISPLAY_NAME,
                    ContactsContract.CommonDataKinds.Email.DATA
                };
                Cursor emailCur =
                    getContentResolver().query(uri, null, null, null,null);
                emailCur.moveToFirst();
                String email = emailCur.getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                String emailType = emailCur.getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
                Toast.makeText(MainActivity.this,
                        "The email is" +email, Toast.LENGTH_LONG).show();
                emailCur.close();
            }
        }
        catch(Exception e) {
            e.getCause();
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您需要执行另一个查询来检索电子邮件地址 检查此答案(关于电子邮件地址的底部部分),问题是:get contact info from android contact picker

编辑:代码示例

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

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

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    // ADD IF STATEMENTS HERE TO CHECK the requestCode, resultCode and data

    Uri contactData = data.getData();
    Cursor c = getContentResolver().query(contactData, null, null, null, null);

    // has result
    if (c.moveToFirst()) {
        String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
        String id = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));

        // new query for the mail
        Cursor emailCursor = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", new String[]{id}, null);

        // has result
        if(emailCursor.moveToFirst()) {
            String email = emailCursor.getString(emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
            String emailType = emailCursor.getString(emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));

            Log.d(getClass().getName(), "EMAIL = " + email);
        }

        emailCursor.close();
    }

    c.close();
}

不要忘记在清单中添加READ_CONTACTS权限。