【发布时间】:2019-11-07 23:22:28
【问题描述】:
我正在尝试读取用户的 SMS 消息并获取这些消息的发件人电话号码。当我尝试通过"address" 列获取消息的发件人电话号码时,它会返回文本对话的电话号码(例如,如果我发送消息对于电话号码为 X 的用户,address 列返回 X 而不是 我的电话号码),而不是此人的电话号码发送消息。下面是我的 Kotlin 代码:
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
}
例如:电话号码为 123456789 的用户向您发送消息。我想检索电话号码 123456789。
【问题讨论】:
-
“文本对话的电话号码”到底是什么意思?您能否举例说明您目前正在获得什么以及您真正想要什么?
-
@MikeM。请查看我的更新说明
-
您有自己的号码吗?对于每条消息?您是否可能通过向自己发送消息进行测试?您确定那些显示您号码的日志是来自另一台设备的消息吗?我的意思是,在给定的代码中,您只为每条消息打印
"address"。如果您也打印"body"列,它们仍然不正确吗? -
@MikeM。很抱歉造成混淆,我的意思是如果我用电话号码 X 向用户发送消息,它会返回 X 而不是我的号码。如果我打印“正文”列,我会正确收到消息内容。
-
正确,但您自己的电话号码不会存储在 SMS Provider 中用于传出消息。基本上,它假设您知道自己的号码。您必须通过常规方法获取该号码,然后使用
"type"列来确定您应该使用您的号码还是"address"列。