【问题标题】:Get the email address from a contact从联系人获取电子邮件地址
【发布时间】:2018-03-06 08:59:02
【问题描述】:

我正在尝试获取我在联系人列表中选择的用户的电子邮件地址。我能够获取联系人的 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 不会有所不同,但似乎确实如此。

【问题讨论】:

  • 看看This discussion
  • 谢谢,在我看来,讨论中的那个方法获取了所有有电子邮件地址的联系人,我正在做的是使用意图打开联系人列表,选择一个联系人,然后是详细信息该联系人被返回,因此 onActivityResult 方法。获取设备上的所有联系人,仅获取一个电子邮件帐户,将是一项艰巨的任务
  • 是的,我知道,但提供者是一样的。只需查询ContactsContract.Contacts.CONTENT_URI

标签: android


【解决方案1】:

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

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

}

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

【讨论】:

    【解决方案2】:

    我希望你知道 Permission 部分和全部,只需在获得用户许可后添加此代码,然后在 Edittext 中填充电子邮件字符串,在任何你想要的地方使用它

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

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

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

    【讨论】:

    • 权限没问题,我已经可以从联系人账户中获取姓名和电话号码,我无法获取电子邮件
    • 你试过上面的Manifest.permission.READ_CONTACTS
    【解决方案3】:

    我认为你们中的一些人不明白我想说什么,但这是我找到的解决方案,通过启动从系统联系人列表中获取详细信息的意图来获取选定联系人的联系人详细信息。

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-30
      • 1970-01-01
      • 1970-01-01
      • 2018-11-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多