Ivan Fork
Ivan Fork

Reputation: 822

How to get phone number for each SMS on the device?

I'm getting all phone's SMS with this URI - Telephony.Sms.Inbox.CONTENT_URI

TextBasedSmsColumns contains all columns for a projection.

Cursor is built like this:

cursor = getContentResolver().query(Telephony.Sms.Inbox.CONTENT_URI,
                        new String[] { BaseColumns._ID, TextBasedSmsColumns.ADDRESS },
                        null, null, null);

There is a TextBasedSmsColumns.ADDRESS column that contains a phone number OR a contact name. So how can I always get the phone number for the SMS? Should I use another approach to get all SMS?

Upvotes: 3

Views: 553

Answers (1)

Somasundaram Mahesh
Somasundaram Mahesh

Reputation: 915

This is how I got.

Uri uriSMSURI = Uri.parse("content://sms/inbox");
Cursor cur = getContentResolver().query(uriSMSURI, null, null, null, null);
if (cur != null) {
        while (cur.moveToNext()) {
            String body = cur.getString(cur.getColumnIndexOrThrow("body"));
            //Here you will get all PhoneNumbers
            String phoneNumber = cur.getString(cur.getColumnIndexOrThrow("address"));
        }
        cur.close();
}

Upvotes: 0

Related Questions