3

I was on this topic Android get contact id from thread id where somebody had the exact same question as i'm asking. Unfortunately, lopez.mikhael answered completely not on the target.

How can I get the contact ID with a thread_id from URI content://sms/conversations ? Assuming that it returns :

- thread_id
- snippet
- msg_count

Getting all the contacts is not helping. I've searched in getColumnsNames() from there and I don't see any good column name that would contain a thread_id.

Why is this not documented ? Is it deprecated to use content://sms/ ? Is there any other solution ?

Thank you.

Community
  • 1
  • 1
Sw0ut
  • 741
  • 2
  • 9
  • 29
  • from column name address you can get the contact phone number then using that phone number you can get the contact details – Rajen Raiyarela May 14 '15 at 13:29
  • How can I find this particular contact without any information in `content://sms/conversations` ? – Sw0ut May 14 '15 at 13:35

1 Answers1

2

As mentioned here on this post

You can fetch sms-inbox:

    Uri mSmsinboxQueryUri = Uri.parse("content://sms/inbox");
    String where = "thread_id="+ <thread id you get it from content://sms/conversations>;
    Cursor cursor1 = getContentResolver().query(mSmsinboxQueryUri,new String[] { "_id", "thread_id", "address", "person", "date","body", "type" }, where, null, null);
    String[] columns = new String[] { "address", "person", "date", "body","type" };
    if (cursor1.getCount() > 0) {
        String count = Integer.toString(cursor1.getCount());
        while (cursor1.moveToNext()){
               String address = cursor1.getString(cursor1.getColumnIndex(columns[0]));
               String name = cursor1.getString(cursor1.getColumnIndex(columns[1]));
               String date = cursor1.getString(cursor1.getColumnIndex(columns[2]));
               String msg = cursor1.getString(cursor1.getColumnIndex(columns[3]));
               String type = cursor1.getString(cursor1.getColumnIndex(columns[4]));
        }
    }

You can fetch other sent items by changing the URI.

Uri mSmsinboxQueryUri = Uri.parse("content://sms/sent");

Ones you have phone number you can find contact id using below code

String contactid = null;

ContentResolver contentResolver = getContentResolver();

Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phonenumintolist));

Cursor cursor =  contentResolver.query(
                        uri, 
                        new String[] {PhoneLookup.DISPLAY_NAME, PhoneLookup._ID}, 
                        null, 
                        null, 
                        null);

if(cursor!=null) {
 while(cursor.moveToNext()){
    String contactName = cursor.getString(cursor.getColumnIndexOrThrow(PhoneLookup.DISPLAY_NAME));
    contactid = cursor.getString(cursor.getColumnIndexOrThrow(PhoneLookup._ID));
 }
 cursor.close();
}
if (contactid == null) {
  Toast.makeText(DetailedCallHistory.this, "No contact found associated with this number", Toast.LENGTH_SHORT).show();
}else{
 //You can contact id do what you want to do with it.
}
Community
  • 1
  • 1
Rajen Raiyarela
  • 5,526
  • 4
  • 21
  • 41
  • I already found this answer but didn't find it conclusive. I would like to have only conversations in a first time. Is it possible ? This is for inbox or sent items. You do not answer my question. -- **EDIT** -- or maybe tell me if what I ask is impossible ? – Sw0ut May 14 '15 at 13:44
  • check the update. i have added where clause for getting the inbox sms based on thread_id, from thread_id you get phonenumber and from phonenumber you get contact id – Rajen Raiyarela May 14 '15 at 13:58
  • Thank you for the complete answer, but still not in what I was expecting. In fact, take the example from the Messages application. First you get all the conversations, and then if you click on a conversation, you get the complete received/sent messages for this conversation. I wanted to have the contact ID from the first screen, where it shows all the conversations. Any ideas ? Because this is for contact information when we are inside the conversation. – Sw0ut May 14 '15 at 14:04
  • you have thread id from content://sms/conversations which you are passing in the next query i.e. content://sms/inbox using String where = "thread_id="+ – Rajen Raiyarela May 14 '15 at 14:29
  • Ok. By the way, the code you gave me is deprecated. (we don't use startManagingCursor anymore since it's on the UI thread) – Sw0ut May 14 '15 at 14:30
  • removed startManagingCursor. – Rajen Raiyarela May 14 '15 at 14:37
  • I would like to do this in only one query, dince I don't know how to implement a second one. Is it possible to do like "joins" or "distinct" ? – Sw0ut May 14 '15 at 16:53
  • if u check the post i referred, the questioner too is looking for a single way of doing it. you can go through the question – Rajen Raiyarela May 15 '15 at 05:34
  • Conversation is based on sms/mms from inbox/sent folders, So even if you can see the conversation list in the Messages Application at first, you have to look for the first message of this conversation to have any info about the contact as it's not stored in the Conversation database as you could see. Maybe you should try to understand a bit more how Android, Messages and the database is working, before criticizing Rajen Answers that are pretty good by the way. – tchoum Sep 23 '15 at 13:36