错误的引用到联系人表从短信收件箱收件箱、联系人、错误、短信

2023-09-07 23:40:07 作者:黄桃布丁

我试图找到对应于手机的短信收件箱短信的联络方式。从我的理解的列是一个外键 ContactsContract.Contacts的 _id

I am trying to find the contact details corresponding to an sms from the phone's sms inbox. From my understanding the person column is a foreign key to the _id column of ContactsContract.Contacts.

我的问题是,我从手机短信查询得到错误的值的值。有些 IDS不要在接触表中,有些是指向完全不同的接触。

My problem is that I am getting wrong values for the person value from the sms query. Some person ids don't exist in the contact table and some are pointing to totally different contact.

下面是code,我使用的取值,谁能告诉我名单,如果我错过了一些东西或遇到了类似的问题。

Below is the code that I use for fetching the list of person values, Can anyone tell me if I missed something or faced a similar problem.

与歌Nexus S运行Android的测试4.1.2

Tested with Nexus S running Android 4.1.2

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开发者门户网站(http://developer.android.com/guide/topics/providers/contacts-provider.html),有没有可能是从SMS收件箱中检索到的 id是指代替 ContactsContract.RawContacts ContactsContract.Contacts ,因为我已经做了。

Update: I was looking at some documentation from the Android Developer Portal (http://developer.android.com/guide/topics/providers/contacts-provider.html), Is it possible that the person id that is retrieved from the sms inbox is referring to the ContactsContract.RawContacts instead of ContactsContract.Contacts as I have done.

推荐答案

事实上在短信收件箱的人列指的是 RawContacts 项的ID。通过查询 RawContacts 表中可以找到 CONTACT_ID 联系人表中的特定用户。

Indeed the person column in the sms inbox refers to a RawContacts entry's id. By querying the RawContacts table you can find the contact_id for the Contacts table for the specific user.

您可以试试这个code来从RawContacts表中的接触ID。

You can try this code to get the contact id from the RawContacts table..

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();

这应该解决您的问题,通过在Android的阅读短信行读取正确的联系方式。然而,随着SMS数据提供商未记录和手机制造商可能有不同的使用它们,就有机会,这种做法可能无法在不是纯粹的Andr​​oid其它手机。

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.