【问题标题】:Wrong reference to Contacts table from sms inbox短信收件箱中对联系人表的错误引用
【发布时间】:2013-01-13 20:23:59
【问题描述】:

我正在尝试从手机的短信收件箱中查找与短信对应的联系方式。据我了解,person 列是ContactsContract.Contacts_id 列的外键。

我的问题是我从短信查询中得到了错误的 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 Portal (http://developer.android.com/guide/topics/providers/contacts-provider.html) 的一些文档,从短信收件箱中检索到的person id 是否可能指的是ContactsContract.RawContacts 而不是ContactsContract.Contacts正如我所做的那样。

【问题讨论】:

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

标签: android message android-contacts


【解决方案1】:

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

您可以尝试使用此代码从 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();

这应该可以解决您通过在 Android 中读取短信行来获取正确联系方式的问题。但是,由于没有记录短信数据提供程序,并且手机制造商可能会以不同的方式使用它们,因此这种方法可能不适用于纯 android 以外的手机。

【讨论】:

    【解决方案2】:

    您首先检查联系人列表中是否存在人员号码。如果是,则匹配两个 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(); 
                            } 
                        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多