Wrong reference to Contacts table from sms inbox

本秂侑毒 提交于 2019-12-10 13:59:16

问题


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.

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.

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.

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

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.


回答1:


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.

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

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.




回答2:


You first check that person number is exits in contact list or not.if yes then match both 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(); 
                        } 
                    }


来源:https://stackoverflow.com/questions/14596908/wrong-reference-to-contacts-table-from-sms-inbox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!