Search code examples
androidandroid-contentprovider

Android sms reading by number


I am working on a sms application. in my App I have to list all the convrsation .SO i use the following Uri.

content://mms-sms/conversations/

Its working fine.And the following is my code snippet.

 Uri uri = Uri.parse("content://mms-sms/conversations/");
 Cursor c= getContentResolver().query(uri, null, null ,null,null);
 startManagingCursor(c);
 if(c.moveToFirst()){

            for(int i=0;i<c.getCount();i++){
                     body[i]= c.getString(c.getColumnIndexOrThrow("body")).toString();
                     number[i]=c.getString(c.getColumnIndexOrThrow("address")).toString();
                     c.moveToNext();
             }
    }
    c.close();

    for (int i = 0; i < body.length; i++) {
        System.out.println("body ="+body[i]);
        System.out.println("number ="+number[i]);
    }

The number prints each conversations number and body prints the each conversations last message. But i want to get each conversations whole message and also help me to conversation for a specific number.


Solution

  • From Conversationscursor, You can get thread_id. Once you got thread_id, continue query from sms provider to return all message which has the same thread_id

    Example : thread_id = 2

    Uri SMS_INBOX = Uri.parse("content://sms");
    Cursor c = getContentResolver().query(SMS_INBOX, null, "thread_id" + " = "
                    + "2", null,
                    "date" + " ASC");
    
    
    while (c.moveToNext()){
    
            Log.v("BODY", c.getString(11).toString()); // 11 is the index of body with project URI
    
        }