从电话获取一些联系信息数据

时间:2019-01-05 21:00:43

标签: java android android-contacts contactscontract

我需要获取 id 名字姓氏数字(或数字)的列表,电子邮件网站。我知道通过获取ID可以查询电话号码,这不是什么大问题。但是我不知道如何查询才能正确获取所有这些列。

我应该为我的名字输入名称,我需要 ContactsContract.CommonDataKinds.StructuredName ,我需要 ContactsContract.CommonDataKinds.Email.ADDRESS ,ID为 Contacts 以及网站 ContactsContract.CommonDataKinds.Website.URL

我的代码返回奇怪的值,例如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...                
}

2 个答案:

答案 0 :(得分:0)

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

相反,您查询包含所有信息的Data.CONTENT_URI表,并通过Data.MIMETYPE的值来区分不同数据类型的行。

在此处查看文档:{​​{3}}

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

这里是获取单个联系人信息的代码(您需要在此处为​​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();

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

答案 1 :(得分:0)

这是我的解决方案: 首先,我们需要一个查询以获取“ raw_contacts”表中的所有联系人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);

然后我们查询表“数据”以获取每个联系人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;
        }

这不是我的完整代码,只是为了获得想法。希望这会对某人有所帮助!