简体   繁体   中英

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?

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM