从 phone 获取一些联系信息数据

Get some contact info data from phone

我需要获取 id名字姓氏号码(或号码)电子邮件网站 android 设备联系人。我知道通过获取 id 我可以查询 phone 个数字,这没什么大不了的。但是我不知道应该如何查询才能正确获取所有这些列。

我想名字我需要 ContactsContract.CommonDataKinds.StructuredName 电子邮件我需要 ContactsContract.CommonDataKinds.Email.ADDRESS ,id 联系人 和网站 ContactsContract.CommonDataKinds.Website.URL.

我的代码 returns 奇怪的值,例如 GIVEN_NAME 的单个数字,FAMILY_NAME 的空值,Email.ADDRESS 的联系电话之一。

不过我认为问题出在查询 URI 上,我应该使用哪一个?

ContentResolver cr = getActivity().getContentResolver();

String[] projection = new String[] {
                    Contacts._ID,
                  ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME,
  ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME,
                    ContactsContract.CommonDataKinds.Website.URL,
                    ContactsContract.CommonDataKinds.Email.ADDRESS};

Cursor nameCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                    projection,
                    null,
                    null,
                    null);
while (nameCur.moveToNext()) {
   String given = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME));
   String family = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME));
   String email = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.ADDRESS));
   Integer id= nameCur.getInt(nameCur.getColumnIndex(Contacts._ID));
   String website = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.Website.URL));
//do some work with strings...                
}

问题是您尝试访问的所有子table(EmailPhoneWebsite 等)实际上都是存储在一个名为 Data 的大 table 中。 所有这些子 table 共享相同的字段 Data1-Data15,因此通过查询 Phone.CONTENT_URI table,您只会获得 Phone 信息,这就是为什么要尝试结果上访问StructuredName.GIVEN_NAME是错误的。

而是查询包含所有信息的 Data.CONTENT_URI table,并通过 Data.MIMETYPE.

处的值区分不同数据类型的行

在此处查看文档:https://developer.android.com/reference/android/provider/ContactsContract.Data

我不确定您是需要设备上所有联系人的联系人信息,还是特定联系人的信息。

这是获取单个联系人信息的代码(您需要为那里的 contactId 变量赋值)

String[] projection = {Data.CONTACT_ID, Data.DISPLAY_NAME, Data.MIMETYPE, Data.DATA1, Data.DATA2, Data.DATA3};

// query only emails/phones/events
String selection = Data.CONTACT_ID + "=" + contactId + " AND " + 
                   Data.MIMETYPE + " IN ('" + StructuredName.CONTENT_ITEM_TYPE + "', '" + Phone.CONTENT_ITEM_TYPE + "', '" + Email.CONTENT_ITEM_TYPE"', '" + Website.CONTENT_ITEM_TYPE + "')";
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(Data.CONTENT_URI, projection, selection, null, null);

while (cur.moveToNext()) {
    long id = cur.getLong(0);
    String name = cur.getString(1); // full name
    String mime = cur.getString(2); // type of data (phone / birthday / email)
    String data = cur.getString(3); // the actual info, e.g. +1-212-555-1234

    String kind = "unknown";

    switch (mime) {
        case StructuredName.CONTENT_ITEM_TYPE: 
            String firstName = cur.getString(4);
            String lastName = cur.getString(5);
            Log.d(TAG, "got name: " + data + " - " + firstName + " " + lastName);
            break;
        case Phone.CONTENT_ITEM_TYPE: 
            Log.d(TAG, "got phone: " + data);
            break;
        case Email.CONTENT_ITEM_TYPE: 
            Log.d(TAG, "got email: " + data);
            break;
        case Website.CONTENT_ITEM_TYPE: 
            Log.d(TAG, "got website: " + data);
            break;
    }
}
cur.close();

要将其转换为所有联系人的 运行,只需从 selection 中删除 Data.CONTACT_ID + "=" + contactId 部分,然后您需要创建一些从 contactId 到对象的 HashMap包含有关该联系人的信息(使用 id 变量作为密钥)

这是我的解决方案: 首先我们需要查询 "raw_contacts"

的 table 中的所有联系人 ID
List<Integer> ret = new ArrayList<Integer>();
ContentResolver contentResolver = getActivity().getContentResolver();
// Row contacts content uri( access raw_contacts table. ).
Uri rawContactUri = ContactsContract.RawContacts.CONTENT_URI;
// Return _id column in contacts raw_contacts table.
String queryColumnArr[] = {ContactsContract.RawContacts._ID};
// Query raw_contacts table and return raw_contacts table _id.
Cursor cursor = contentResolver.query(rawContactUri, queryColumnArr, null, null, null);

然后我们查询 table "data" 以获取每个联系人 ID 的额外信息:

// Data content uri (access data table. )
Uri dataContentUri = ContactsContract.Data.CONTENT_URI;
// Build query columns name array.
List<String> queryColumnList = new ArrayList<String>();
// ContactsContract.Data.CONTACT_ID = "contact_id";
queryColumnList.add(ContactsContract.Data.CONTACT_ID);
// ContactsContract.Data.MIMETYPE = "mimetype";
queryColumnList.add(ContactsContract.Data.MIMETYPE);
queryColumnList.add(ContactsContract.Data.DATA1);
queryColumnList.add(ContactsContract.Data.DATA2);
queryColumnList.add(ContactsContract.Data.DATA3);
queryColumnList.add(ContactsContract.Data.DATA4);
queryColumnList.add(ContactsContract.Data.DATA5);
queryColumnList.add(ContactsContract.Data.DATA6);
queryColumnList.add(ContactsContract.Data.DATA7);
queryColumnList.add(ContactsContract.Data.DATA8);
queryColumnList.add(ContactsContract.Data.DATA9);
queryColumnList.add(ContactsContract.Data.DATA10);
queryColumnList.add(ContactsContract.Data.DATA11);
queryColumnList.add(ContactsContract.Data.DATA12);
queryColumnList.add(ContactsContract.Data.DATA13);
queryColumnList.add(ContactsContract.Data.DATA14);
queryColumnList.add(ContactsContract.Data.DATA15);
// Translate column name list to array.
String queryColumnArr[] = queryColumnList.toArray(new String[queryColumnList.size()]);
// Build query condition string. Query rows by contact id.
StringBuffer whereClauseBuf = new StringBuffer();
whereClauseBuf.append(ContactsContract.Data.RAW_CONTACT_ID);
whereClauseBuf.append("=");
whereClauseBuf.append(rawContactId);
// Query data table and return related contact data.
Cursor cursor = contentResolver.query(dataContentUri, queryColumnArr, whereClauseBuf.toString(), null, null);

最后一个光标包含任何联系人的所有数据,要获取该数据,我们需要切换大小写:

String mimeType =cursor.getString(cursor.getColumnIndex(ContactsContract.Data.MIMETYPE));
switch (mimeType) {
            // Get email data.
            case ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE:
                // Email.ADDRESS == data1
                String emailAddress = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.ADDRESS));
                int emailType = cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
                data.setDataType(emailType);
                data.setDataValue(emailAddress);
                ret1.add(data);
                con.setEmailList(ret1);
                break;
            // Get organization data.
            case ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE:
                // Organization.COMPANY == data1
                String company = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Organization.COMPANY));
             con.setCompany(company);
                break;
            // Get phone number.
            case ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE:
                // Phone.NUMBER == data1
                String phoneNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                // Phone.TYPE == data2
                int phoneTypeInt = cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
                data.setDataType(phoneTypeInt);
                data.setDataValue(phoneNumber);
                ret1.add(data);
                con.addPhoneList(ret1);
                break;
            // Get display name.
            case ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE:
                // StructuredName.DISPLAY_NAME == data1
                String displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME));
                // StructuredName.GIVEN_NAME == data2
                String givenName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME));
                // StructuredName.FAMILY_NAME == data3
                String familyName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME));
                ret.add("Display Name : " + displayName);
                ret.add("Given Name : " + givenName);
                ret.add("Family Name : " + familyName);           
                break;
            // Get website.
            case ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE:
                // Website.URL == data1
                String websiteUrl = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Website.URL));
                // Website.TYPE == data2
                int websiteTypeInt = cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Website.TYPE));
                String websiteTypeStr = getEmailTypeString(websiteTypeInt);
                ret.add("Website Url : " + websiteUrl);
                ret.add("Website Type Integer : " + websiteTypeInt);
                ret.add("Website Type String : " + websiteTypeStr);
                break;
        }

这不是我的完整代码,只是为了了解一下。希望这会对某人有所帮助!