Alex Styl
Alex Styl

Reputation: 4219

Getting phone number of each sms via content://sms/

I've wrote the following code, to get the whole conversation between the user and a number:

Uri SMS_INBOX = Uri.parse("content://sms/");
        String selection = "thread_id = " + thread_id;
        final String[] projection = new String[] { "*" };
        Cursor c = getContentResolver().query(SMS_INBOX, projection, selection,null, "date");

        startManagingCursor(c);

        String[] body = new String[c.getCount()];
        String[] address = new String[c.getCount()];
        if (c.moveToFirst()) {
            for (int j = 0; j < c.getColumnCount(); j++)
                Log.w("ColumnName", c.getColumnName(j));
            for (int i = 0; i < c.getCount(); i++) {
                body[i] = c.getString(c.getColumnIndexOrThrow("body")).toString();
                address[i] = c.getString(c.getColumnIndexOrThrow("address")).toString();
                Log.d("address-" + i, address[i]);
                Log.d("body-" + i, body[i]);
                String subject =  c.getString(c.getColumnIndexOrThrow("_id")).toString();
                Log.d("_id-" + i, subject);
                String thread =  c.getString(c.getColumnIndexOrThrow("thread_id")).toString();
                Log.d("thread_id-" + i, subject);
                Log.d("----", "----");

                c.moveToNext();
            }

        }

Via this code, i get all the messages in a conversation. The problem is, I can't figure out which number is sending which message. If i get the column "address" it returns the same number all the time (actually it returns the other person's number only), so I can't keep record of whether the message I just got through this code was sent by the user or the other number.

Upvotes: 0

Views: 842

Answers (1)

sarath
sarath

Reputation: 3201

The column will always gives second persons number only.If you want to differentiate sent message and received message you have to use column 'type'.

body[i] = c.getString(c.getColumnIndexOrThrow("body")).toString();

if(c.getString(c.getColumnIndex("type")).equalsIgnoreCase("1")){

                // sms received

                msg_state[i]=1;

             }

              else {
                //sms sent
                msg_state[i]=0;

             }

No You can easily identify the sent sms and received sms.

Upvotes: 1

Related Questions