从联系人处获取电子邮件地址

时间:2018-03-06 08:59:02

标签: android

我试图获取我在联系人列表中选择的用户的电子邮件地址。我能够获取联系人的ID,电话和姓名,并使用ID,我试图使用新的光标获取电子邮件地址,但我无法从联系人获取详细信息。

以下是我正在使用的代码:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent aData) {
    super.onActivityResult(requestCode, resultCode, aData);
    if (requestCode == PICK_CONTACT && resultCode == Activity.RESULT_OK && aData != null) {

        ContentResolver cr = getContentResolver();
        String phoneNo = "";
        String name = "";
        String id = "";
        String email = "";
        Uri uri = aData.getData();
        if (uri != null) {
            Cursor cursor = cr.query(uri, null, null, null, null);
            if (cursor != null) {
                if (cursor.moveToFirst()) {
                    id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
                    int phoneIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
                    int nameIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
                    phoneNo = cursor.getString(phoneIndex);
                    name = cursor.getString(nameIndex);
                }
                cursor.close();
            } else {
                showDialog(getString(R.string.lbl_error_message_contact));
            }
        } else {
            showDialog(getString(R.string.lbl_error_message_contact));
        }
        Cursor emailCur = cr.query(
                ContactsContract.CommonDataKinds.Email.CONTENT_URI,
                null,
                ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
                new String[]{id}, null);
        while (emailCur.moveToNext()) { // when it gets here, it just skips the while loop and jumps down to the to close the emailCur
            // This would allow you get several email addresses
            // if the email addresses were stored in an array
            email = emailCur.getString(
                    emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.ADDRESS));
        }
        emailCur.close();

        mMedia.setLendersName(name);
        mMedia.setPhoneNumber(phoneNo);
        mMedia.setEmail(email);


    }
 }

可能是ID可能不正确吗?

修改

一件非常奇怪的事情是,当我选择一个联系人并返回到屏幕时。该ID与我运行@Android Team代码时获得的ID非常不同,后者返回所有具有电子邮件地址的联系人。这背后的原因可能是什么?

当我试图获取电子邮件地址时,我的想法是ID不会有所不同,但似乎是这种情况。

3 个答案:

答案 0 :(得分:0)

获取设备联系信息时,您可以在清单文件中添加权限..

<uses-permission android:name="android.permission.READ_CONTACTS"/>

然后使用以下方法获取设备存储的所有电子邮件ID ..

    /**
 * this method read device in contact name and email.
 */
public void getContact() {
        Cursor cur = getActivity().getContentResolver().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));
                Cursor cur1 = getActivity().getContentResolver().query(
                        ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
                        ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
                        new String[]{id}, null);
                while (cur1.moveToNext()) {
                    //to get the contact names
                    String name = cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                    Log.e("Name :", name);
                    String email = cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                    Log.e("Email", email);
                    }
                cur1.close();
            }
        }

}

签入log cat显示要在设备中添加的所有电子邮件ID。

答案 1 :(得分:0)

我希望您知道Permission部分和所有内容,只需在用户允许的情况下添加此代码,并在Edittext中填充电子邮件字符串,随时随地使用

Account[] accounts = AccountManager.get(activityContext).getAccountsByType("com.google");
                    for (Account account : accounts) {
                        // if (emailPattern.matcher(account.name).matches()) {
                        String possibleEmail = account.name;
                    }

您可能必须在活动中使用这段代码来填充数据,只需添加一个

runOnUiThread(new Runnable() {
        public void run() {
            regEmail.setText(emailId);
        }
    });

答案 2 :(得分:0)

我不认为你们中的一些人理解我想说的是什么,但这是我通过开始意图从系统联系人列表中获取详细信息来获取所选联系人的联系人详细信息的解决方案。 / p>

使用以下意图启动意图以获取姓名和电话号码:

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

选择联系人后,它将返回onActivityResult。以下是从响应中获取姓名和电话号码的示例。

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent aData) {
    super.onActivityResult(requestCode, resultCode, aData);

    if (requestCode == PICK_CONTACT && resultCode == Activity.RESULT_OK && aData != null) {
        final String[] projection = new String[]{ContactsContract.CommonDataKinds.Email.DATA, ContactsContract.CommonDataKinds.Email.TYPE};
        ContentResolver cr = getContentResolver();
        String phoneNo;
        String name;
        String lookUpKey = "";
        String email;
        Uri uri = aData.getData();
        if (uri != null) {
            // ****************************First we want to get the Lookup Key, Name and Phone number****************************
            Cursor cursor = cr.query(uri, null, null, null, null);
            if (cursor != null) {
                if (cursor.moveToFirst()) {
                    lookUpKey = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
                    int phoneIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
                    int nameIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
                    phoneNo = cursor.getString(phoneIndex);
                    name = cursor.getString(nameIndex);
                    mMedia.setLendersName(name);
                    mMedia.setPhoneNumber(phoneNo);
                }
                // ****************************Now we want to get the email address, if any from the contact****************************
                // At this point, we use the Lookup key of the contact above, to get the email address (If any)
                if (!TextUtils.isEmpty(lookUpKey)) {
                    //Do a query with the Lookup key to get the email details (Could possibly get more data as well, but I just needed the email address
                    cursor = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, projection, ContactsContract.Data.LOOKUP_KEY + "=?", new String[]{lookUpKey}, null);
                    if (cursor != null) {
                        while (cursor.moveToNext()) {
                            // Get the index of the column for the email address here
                            final int contactEmailColumnIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA);

                            while (!cursor.isAfterLast()) {
                                // Here is where we get the email address itself
                                email = cursor.getString(contactEmailColumnIndex);
                                // Set it where ever you need it.
                                mMedia.setEmail(email);
                                cursor.moveToNext();
                            }
                        }
                        // Dont forget to close the cursor once you done with it
                        cursor.close();
                    }
                }
            } else {
                showDialog(getString(R.string.lbl_error_message_contact));
            }
        } else {
            showDialog(getString(R.string.lbl_error_message_contact));
        }
    }
}