5

我正在尝试从手机的短信收件箱中查找与短信相对应的联系方式。据我了解,该person_idContactsContract.Contacts.

我的问题是我person从短信查询中得到了错误的值。person联系人表中不存在某些id,而有些则指向完全不同的联系人。

下面是我用于获取person值列表的代码,谁能告诉我我是否遗漏了什么或遇到了类似的问题。

使用运行 Android 4.1.2 的 Nexus S 进行测试

Uri parsedUri = Uri.parse("content://sms/inbox");
    String[] projection = new String[] { "_id", "address", "person" };
    Cursor cursor = contentResolver.query(parsedUri, projection, null, null, null);

    Log.d(TAG, "Total Count " + cursor.getCount());

    if (cursor.getCount() > 0) {
        // String address;
        int person;
        int personIndex = cursor.getColumnIndex("person");
        if (cursor.moveToFirst())
            do {
                person = cursor.getInt(personIndex);
                if (person > 0) {
                    // Add this id to a list
                }
            } while (cursor.moveToNext());
    }
    cursor.close();

更新:我正在查看Android Developer Portalhttp://developer.android.com/guide/topics/providers/contacts-provider.html)的一些文档,是否有可能person从短信收件箱中检索到的 id 指的是而ContactsContract.RawContacts不是ContactsContract.Contacts我所做的。

4

2 回答 2

5

实际上,短信收件箱中的人员列指的是RawContacts条目的 ID。通过查询 RawContacts表,您可以找到特定用户contact_idContacts表。

您可以尝试使用此代码从 RawContacts 表中获取联系人 ID。

long contactId = 0;
Uri uri = ContactsContract.RawContacts.CONTENT_URI;
String[] projection = new String[] { ContactsContract.RawContacts._ID, ContactsContract.RawContacts.CONTACT_ID };
Cursor cursor = contentResolver.query(uri, projection, 
        ContactsContract.RawContacts._ID + " = ?",
        new String[] { rawContactId }, null);

if (cursor.moveToFirst()) {
    int contactIdIndex = cursor.getColumnIndex(ContactsContract.RawContacts.CONTACT_ID);
    contactId = cursor.getLong(contactIdIndex);
}
cursor.close();

This should solve your problem for fetching the correct contact details by reading an sms row in Android. However as the sms data provider is not documented and phone manufacturers may use them differently, there is chance that this approach may not work on phones other than pure android.

于 2013-01-31T06:40:58.597 回答
0

您首先检查联系人列表中是否存在人员号码。如果是,则匹配两个 ID。

Cursor cursor=managedQuery(uri, null, null, null, null);

                    while (cursor.moveToNext()) { 
                        String contactId = cursor.getString(cursor.getColumnIndex( 
                                ContactsContract.Contacts._ID)); 
                        String hasPhone = cursor.getString(cursor.getColumnIndex( 
                                ContactsContract.Contacts.HAS_PHONE_NUMBER)); 
                        if (Boolean.parseBoolean(hasPhone)) { 
                            // You know have the number so now query it like this
                            Cursor phones = getContentResolver().query( 
                                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
                                    null, 
                                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId, 
                                    null, null); 
                            while (phones.moveToNext()) { 
                                String phoneNumber = phones.getString( 
                                        phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));              
                            } 
                            phones.close(); 
                        } 
                    }
于 2013-01-30T05:05:00.693 回答