1

我正在开发一个处理联系人和日历的应用程序。

我在使用特定设备 Sony Xperia SP 时遇到了困难,但我找不到其他人有同样的问题。

我正在尝试使用 Android Api 中可用的实体 Uri 获取设备联系人的显示名称和电话号码:http: //developer.android.com/reference/android/provider/ContactsContract.RawContacts.Entity。 html

不幸的是,这段代码在三星 Galaxy S4 上运行良好,在那里我得到了我正在查询的所有细节,但是在索尼 Xperia SP 上,我得到了一个非法参数异常,在堆栈跟踪中详细说明了:无效的列名显示姓名。

下面是我正在使用的代码:

ContentResolver cr = context.getContentResolver();
Uri rawUri = RawContacts.CONTENT_URI;
String[] rawProjection = { RawContacts._ID };
String rawSelection = RawContacts.ACCOUNT_NAME + " = ? AND " + RawContacts.ACCOUNT_TYPE + " = ?";
String[] rawSelectionArgs = new String[] { account.name, account.type };

    Cursor rawCursor = cr.query(rawUri, rawProjection, rawSelection, rawSelectionArgs, null);

    while (rawCursor.moveToNext()) {

        long contactId = rawCursor.getLong(0);

        Uri entityUri = Uri.withAppendedPath(ContentUris.withAppendedId(rawUri, contactId), Entity.CONTENT_DIRECTORY);
        String[] entityProjection = new String[] { Data.DISPLAY_NAME, CommonDataKinds.Phone.NORMALIZED_NUMBER };
        String entitySelection = Entity.MIMETYPE + " = ?";
        String[] entitySelectionArgs = new String[] { CommonDataKinds.Phone.CONTENT_ITEM_TYPE };
        Cursor entityCursor = cr.query(entityUri, entityProjection, entitySelection, entitySelectionArgs, null);

        while (entityCursor.moveToNext()) {
            String name = entityCursor.getString(entityCursor.getColumnIndex(Data.DISPLAY_NAME));
            String number = entityCursor.getString(entityCursor.getColumnIndex(CommonDataKinds.Phone.NORMALIZED_NUMBER));
        }

        entityCursor.close();

    }

我试图将我的 entityQuery 的投影设置为 null,以查找此查询返回的字段,实际上,返回的字段在设备上并不相同,

有谁知道为什么这些字段在 Sony Xperia SP 上不可见,我可以在那里使用哪种解决方法?

4

1 回答 1

-1

确保您已授予读取联系人权限。选择联系人时,我有同样的问题。

<uses-permission android:name="android.permission.READ_CONTACTS" />
于 2015-04-01T18:32:34.597 回答