1

I'm working on a simple sms app and I'm using the code below to get the thread id when loading my threads list but I can't figure out how to get the contact id using the thread id. I'm root and using root explorer I can see in the database there is a contacts table with the following columns

thread_id | htcthread_id | contact_id

So since I have the thread id I should be able to get the contact id but I also need to make sure this works on all devices. My app is not root by the way

code to get thread id

Uri uri = Uri.parse("content://mms-sms/conversations?simple=true");
Cursor c = context.getContentResolver().query(uri, null, null, null, "date desc");
if (c.getCount() > 0) {
    while (c.moveToNext()){
        //thread id is c.getString(c.getColumnIndexOrThrow("_id"))
    }
}
c.close
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188
user577732
  • 3,956
  • 11
  • 55
  • 76

1 Answers1

1

My solution to recover all contacts:

    Cursor cursor = null;
    try {
        cursor = context.getContentResolver().query(Phone.CONTENT_URI, null, null, null, null);
        int contactIdIdx = cursor.getColumnIndex(Phone._ID);
        int nameIdx = cursor.getColumnIndex(Phone.DISPLAY_NAME);
        int phoneNumberIdx = cursor.getColumnIndex(Phone.NUMBER);
        int photoIdIdx = cursor.getColumnIndex(Phone.PHOTO_ID);
        cursor.moveToFirst();
        do {
            String idContact = cursor.getString(contactIdIdx);
            String name = cursor.getString(nameIdx);
            String phoneNumber = cursor.getString(phoneNumberIdx);
            //...
        } while (cursor.moveToNext());  
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }

You need this permission in your manifest :

<uses-permission android:name="android.permission.READ_CONTACTS" />

I hope I have helped you!

lopez.mikhael
  • 9,943
  • 19
  • 67
  • 110
  • 4
    okay but how does that answer my question? I'm not trying to get all of the contacts I just want the contact information based off of a thread id – user577732 May 23 '13 at 07:35