5

I am trying to read a user's SMS messages and get the sender's phone number of those messages. When I try getting the sender's phone number of the message through the "address" column, it returns the phone number of the text's conversation (for example, if I send a message to a user with phone number X, the address column returns X instead of my phone number), not the phone number of the person that sent the message. Below is my Kotlin code:

var cursor = contentResolver.query(
    Uri.parse("content://sms/"),
    null,
    null,
    null,
    null
)

// Retrieve the IDs of the sender's name
var senderID = cursor!!.getColumnIndex("address")

// Iterate through every message
while (cursor!!.moveToNext()) {
    var messageSender = cursor.getString(senderID) // Get the sender of the message
    System.out.println("---------------------------------------------------------")
    System.out.println(messageSender) // Returns phone number of the conversation, not the sender
}

For example: user with phone number 123456789 sends a message to you. I want to retrieve phone number 123456789.

Adam Lee
  • 436
  • 1
  • 14
  • 49
  • What exactly do you mean by "the phone number of the text's conversation"? Can you please give an example of what you're currently getting, and what you actually want? – Mike M. Nov 10 '19 at 07:20
  • @MikeM. Please see my updated description – Adam Lee Nov 10 '19 at 07:42
  • You're getting your own number? For every message? Have you possibly been testing by sending yourself messages? Are you sure that those logs that show your number are for messages from another device? I mean, in the given code, you're printing only the `"address"` for each message. If you print the `"body"` column, too, are they still not correct? – Mike M. Nov 10 '19 at 08:20
  • @MikeM. Sorry for the confusion, I meant to say that if I send a message to a user with phone number X, it returns X instead of my number. If I print the "body" column, I correctly receive the message contents. – Adam Lee Nov 10 '19 at 08:29
  • Oh, OK. Well, it's not going to save your number, even if it's an outgoing message that you've sent. It only saves the number of the other party, the other device. (If you were to send a message to yourself, then you should see your number on both the sent and received messages.) What are you trying to determine, exactly? I mean, are you just trying to differentiate messages that you've sent from those that you've received? – Mike M. Nov 10 '19 at 08:41
  • @MikeM. I am trying to determine who sent the messages. So if I send a message to someone, my number should be returned. If someone with phone number X sends a message to me, that phone number X should be returned. – Adam Lee Nov 10 '19 at 08:42
  • For that, you simply check the `"type"` (`Telephony.Sms.TYPE`) column. Messages that you've sent will have type `2` (`Telephony.Sms.MESSAGE_TYPE_SENT`). Received messages have type `1` (`Telephony.Sms.MESSAGE_TYPE_INBOX`). You can also query for just one type at a time, if you'd like. There are URI "shortcuts" for that, so you don't have to do a `WHERE` on the type. For example, if you change your URI to `"content://sms/inbox"`, you'll get only received messages. – Mike M. Nov 10 '19 at 08:51
  • @MikeM. I'm not just trying to get whether or not the message is one that I sent or received, but I am trying to get the actual phone number of whoever sent the message (e.x. 123-456-78910) – Adam Lee Nov 10 '19 at 09:21
  • 4
    Right, but your own phone number is not going to be stored for outgoing messages in the SMS Provider. Basically, it's assuming that you know your own number. You'll have to get that number through the usual methods, and then use the `"type"` column to determine whether you should using your number, or the `"address"` column. – Mike M. Nov 10 '19 at 09:31
  • this is conceptually already answered here regardless kotlin or java: https://stackoverflow.com/questions/19451132/android-how-to-get-sender-and-receiver-phone-number-from-raw-sms/25469343 – Maytham Fahmi Nov 15 '19 at 13:51

1 Answers1

1

I found a solution. You must use the type column to identify whether the message has been sent or received.
When the message has been sent, you can read the phone number from the Telephony Manager.

fun getMessageSender(cursor: Cursor): String {
        val partnerAddressId = cursor.getColumnIndex("address")
        val typeId = cursor.getColumnIndex("type")

        val partnerAddress = cursor.getString(partnerAddressId)
        val type = cursor.getString(typeId)

        return if (type.equals("1", true)) {
            partnerAddress
        } else {
            getPhoneNumber()
        }
    }
private fun getPhoneNumber(): String {
        val telephonyManager = getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
        return telephonyManager.line1Number
    }

But please note that I do not know how this affects devices with Multi Sim. I could imagine that the wrong number will be returned here.

I have worked out this solution in combination with the following posts:
Getting phone number of each sms via content://sms/
Programmatically obtain the phone number of the Android phone

Robin
  • 561
  • 1
  • 4
  • 16