我在从手机联系人加载电子邮件地址时遇到问题

发布于 2024-11-06 13:17:37 字数 1447 浏览 0 评论 0原文

我正在尝试从手机联系人生成电子邮件列表,但在专门获取电子邮件方面遇到了麻烦。我可以获取姓名和电话号码,但不能获取电子邮件。循环结束时 ArrayList 大小均为 0。谁能发现我做错了什么,或者告诉我电子邮件与电话号码、姓名和其他信息有何不同?

    ContentResolver cr = getContentResolver();

    Cursor emailCur = cr.query(
            ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
            ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
            null, null);

    ArrayList<String> emails = new ArrayList<String>();
    ArrayList<String> emailTypes = new ArrayList<String>();

    while (emailCur.moveToNext()) {
        // This would allow you get several email addresses
        // if the email addresses were stored in an array
        String email = emailCur
                .getString(emailCur
                        .getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
        String emailType = emailCur
                .getString(emailCur
                        .getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));

        emails.add(email);
        emailTypes.add(emailType);

    }

    emailCur.close();

    Log.i("Emails Count", Integer.toString(emails.size()));
    for (int i = 0; i < emails.size(); i++) {
        Log.i("Emails " + i, emails.get(i));
    }

    Log.i("EmailTypes Count", Integer.toString(emailTypes.size()));
    for (int i = 0; i < emailTypes.size(); i++) {
        Log.i("EmailTypes " + i, emailTypes.get(i));
    }

I am trying to generate a list of emails from the phone's contacts but I have having trouble specifically getting emails. I can get names and phone numbers but not emails. The ArrayList sizes are both 0 at the end of the loop. Can anyone spot what I am doing wrong, or tell me how emails differ from phone numbers, names and other information?

    ContentResolver cr = getContentResolver();

    Cursor emailCur = cr.query(
            ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
            ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
            null, null);

    ArrayList<String> emails = new ArrayList<String>();
    ArrayList<String> emailTypes = new ArrayList<String>();

    while (emailCur.moveToNext()) {
        // This would allow you get several email addresses
        // if the email addresses were stored in an array
        String email = emailCur
                .getString(emailCur
                        .getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
        String emailType = emailCur
                .getString(emailCur
                        .getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));

        emails.add(email);
        emailTypes.add(emailType);

    }

    emailCur.close();

    Log.i("Emails Count", Integer.toString(emails.size()));
    for (int i = 0; i < emails.size(); i++) {
        Log.i("Emails " + i, emails.get(i));
    }

    Log.i("EmailTypes Count", Integer.toString(emailTypes.size()));
    for (int i = 0; i < emailTypes.size(); i++) {
        Log.i("EmailTypes " + i, emailTypes.get(i));
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

百善笑为先 2024-11-13 13:17:37

使用此代码从设备获取电子邮件。

ContentResolver cr = getContentResolver();
                 cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,null, null, null);

                emailIndex=0;
                if (cur.getCount() > 0) 
                {

                    while (cur.moveToNext()) 
                    {
                        String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
                        name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

                        Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,null,
                                ContactsContract.CommonDataKinds.Email.CONTACT_ID+ " = " + id, null, null);
                        while (emails.moveToNext()) 
                        {
                            // This would allow you get several email addresses
                            String emailAddress = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                            Log.v(name+"==>", emailAddress);

                        }
                        emails.close();         
                    }
                    cur.close();

use this code to get email from device.

ContentResolver cr = getContentResolver();
                 cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,null, null, null);

                emailIndex=0;
                if (cur.getCount() > 0) 
                {

                    while (cur.moveToNext()) 
                    {
                        String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
                        name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

                        Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,null,
                                ContactsContract.CommonDataKinds.Email.CONTACT_ID+ " = " + id, null, null);
                        while (emails.moveToNext()) 
                        {
                            // This would allow you get several email addresses
                            String emailAddress = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                            Log.v(name+"==>", emailAddress);

                        }
                        emails.close();         
                    }
                    cur.close();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文