3

I would like to get all messages from one contact. I have the contact ID _id, so I've already do something like this :

private void displayMessages() {
    ContentResolver cr = this.getContentResolver();


    try {
        Cursor cursor = cr.query(Uri.parse("content://mms-sms/conversations"), new String[]{"*"}, this.contID + "= _id" , null, null); //contID is the unique ID for one contact in our android.
        while (cursor.moveToNext()) {
            System.out.println("Number: " + cursor.getString(cursor.getColumnIndexOrThrow("address")));
            System.out.println("Body : " + cursor.getString(cursor.getColumnIndexOrThrow("body")));
        }
        cursor.close();

    }
    catch (Exception e) {
        System.out.print("ERROR");
    }

I've asked something like

SELECT * FROM content://mms-sms/conversations WHERE _id = contID

As you can see, in my query, I ask system for messages from one contact (user id that I know) But I can just display the last message body. So I think it exists others query to get all messages from one user ?

Any ideas ???

Iamat8
  • 3,888
  • 9
  • 25
  • 35
Couim
  • 735
  • 3
  • 12
  • 29
  • http://stackoverflow.com/questions/5946262/read-inbox-messages-of-a-particular-number-and-display-them-in-an-activity http://stackoverflow.com/questions/14721772/get-sms-of-specific-phone-number – Chaudhary Amar Oct 03 '15 at 09:36

2 Answers2

1

I doubt that _id in this case is the id of the contact you are sending sms/mms to but rather the thread's id. The column you probably want to use for your query clause is RECIPIENT_IDS. For debugging purpose try without the where clause and dump the cursor to analyze the results

Cursor cursor = cr.query(Uri.parse("content://mms-sms/conversations"), new String[]{"*"}, null , null, null);
DatabaseUtils.dumpCursor(cursor);
Blackbelt
  • 156,034
  • 29
  • 297
  • 305
1

Sorry for answer too late, but I've found a solution.

Here is my code :

private void displayMessages() {
    ContentResolver cr = this.getContentResolver();
    boolean isMe;

    try {
        Cursor cursor = cr.query(Uri.parse("content://sms"), null, this.cont.getThread_ID() + " = thread_id" , null, "date ASC");

        while (cursor.moveToNext()) {
            //if it's a received message :
            if ((cursor.getString(cursor.getColumnIndexOrThrow("person")) != null)) {
                isMe = false;
            }
            else {
                //it's a message sent by me.
                isMe = true;
            }

            date = cursor.getString(cursor.getColumnIndexOrThrow("date"));
            System.out.println("Number: " + cursor.getString(cursor.getColumnIndexOrThrow("address")));
            System.out.println("Body : " + cursor.getString(cursor.getColumnIndexOrThrow("body") + "\n");
        }
        cursor.close();

    }

Thanks to "conent://sms" table, I can get all sms ! So I've used thread_id to display all messages, and thanks to the person column, I can know if the message was sent by me or my contact.

Couim
  • 735
  • 3
  • 12
  • 29