java - 如何获取电子邮件 ID、显示名称和电话号码

标签 java android

我使用以下查询来获取电话号码、显示名称和电子邮件

String[] PROJECTION = new String[] { Contacts.DISPLAY_NAME, Phone.NUMBER,
            Email.DATA, Contacts._ID };

ContentResolver cr = getContentResolver();
        Cursor cursor = cr.query(ContactsContract.Data.CONTENT_URI, PROJECTION,
                null, null, null);

但没有使用获取电话号码

phonenumber = cursor
                    .getString(cursor
                            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER_ID));

最佳答案

尝试使用..

ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
    "DISPLAY_NAME = '" + NAME + "'", null, null);
if (cursor.moveToFirst()) {
    String contactId =
        cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
    //
    //  Get all phone numbers.
    //
    Cursor phones = cr.query(Phone.CONTENT_URI, null,
        Phone.CONTACT_ID + " = " + contactId, null, null);
    while (phones.moveToNext()) {
        String number = phones.getString(phones.getColumnIndex(Phone.NUMBER));
        int type = phones.getInt(phones.getColumnIndex(Phone.TYPE));
        switch (type) {
            case Phone.TYPE_HOME:
                // do something with the Home number here...
                break;
            case Phone.TYPE_MOBILE:
                // do something with the Mobile number here...
                break;
            case Phone.TYPE_WORK:
                // do something with the Work number here...
                break;
            }
    }
    phones.close();
 }
cursor.close();

或者,参见..

关于java - 如何获取电子邮件 ID、显示名称和电话号码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16498858/

相关文章:

java - 错误 "hours cannot be resolved to a variable",我做错了什么?

java - 当所有其他实例都正确时,为什么我会返回错误的 boolean 值?

android - 在 Android 中将大量叠加层加载到 MapView 的最佳方法是什么?

安卓 : Bitmap : How to save canvas with green background in android?

java - 递归函数无法获取最里面的异常消息

java - 我想从 Firebase 获取产品 ID

java - 如何保证 Java/Spring 应用程序上的 WebSocket 消息顺序

android - ScrollView 内的 ViewPager 内的 Recyclerview 无法正常工作

android - Apk 大小与下载大小

android - 导航架构组件 - Activity