我知道关于短信的一件事......
我知道可以在Android中阅读短信,但我不知道如何阅读"未读短信",特别是如何加入每个字段,如"发件人& #34;,"短信","日期","时间","文字"。
我必须做的是:当收到短信时,应用程序"读取"消息并将所有信息存储在数据结构中。
重要: 另一个问题是:是否可以阅读"未读短信"在WhatsApp或其他IM应用程序(例如facebook messenger)? 在这种情况下,我如何加入每个领域?
此致
答案 0 :(得分:3)
阅读手机短信
查看How can I read SMS messages from the device programmatically in Android?
阅读Facebook消息
对于Facebook,您需要实施他们的API。见Read Messages in Facebook
选项1
Whatsapp没有发布任何官方API。
这个开源API用于与whatsapp通信,它不是官方的,如果Whatsapp更新协议,可能会停止工作。
https://github.com/venomous0x/WhatsAPI
关于使用此官方API或其他非官方API的合法性,这取决于您与Whatsapp达成的服务协议。阅读它,看看他们是否对他们与他们以外的客户使用他们的通信协议不屑一顾。我的猜测是他们不允许这样做。
选项2
WhatsApp每天凌晨4点在您的SD卡上进行聊天备份。这是使用AES密钥加密的单个数据库。由于它位于外部存储设备上,因此如果您有权访问外部存储设备,您的应用程序就可以读取它。
您可以轻松解密此数据库(在线提供了一篇论文)。
但是,这只会在24小时内为您提供最新的聊天记录。
如果您想要更多实时更新,您的设备必须是root用户,并且您需要一个全新的解析器,就像在内部存储上一样,聊天记录存储在多个数据库中。
选项3
阅读此问题Get all messages from Whatsapp
注意:我不确定Whatsapp的内容。这只是各种帖子的汇编。
答案 1 :(得分:0)
最简单的功能
要阅读短信,我编写了一个返回会话对象的函数:
class Conversation(val number: String, val message: List<Message>)
class Message(val number: String, val body: String, val date: Date)
fun getSmsConversation(context: Context, number: String? = null, completion: (conversations: List<Conversation>?) -> Unit) {
val cursor = context.contentResolver.query(Telephony.Sms.CONTENT_URI, null, null, null, null)
val numbers = ArrayList<String>()
val messages = ArrayList<Message>()
var results = ArrayList<Conversation>()
while (cursor != null && cursor.moveToNext()) {
val smsDate = cursor.getString(cursor.getColumnIndexOrThrow(Telephony.Sms.DATE))
val number = cursor.getString(cursor.getColumnIndexOrThrow(Telephony.Sms.ADDRESS))
val body = cursor.getString(cursor.getColumnIndexOrThrow(Telephony.Sms.BODY))
numbers.add(number)
messages.add(Message(number, body, Date(smsDate.toLong())))
}
cursor?.close()
numbers.forEach { number ->
if (results.find { it.number == number } == null) {
val msg = messages.filter { it.number == number }
results.add(Conversation(number = number, message = msg))
}
}
if (number != null) {
results = results.filter { it.number == number } as ArrayList<Conversation>
}
completion(results)
}
使用:
getSmsConversation(this){ conversations ->
conversations.forEach { conversation ->
println("Number: ${conversation.number}")
println("Message One: ${conversation.message[0].body}")
println("Message Two: ${conversation.message[1].body}")
}
}
或仅获得特定号码的会话:
getSmsConversation(this, "+33666494128"){ conversations ->
conversations.forEach { conversation ->
println("Number: ${conversation.number}")
println("Message One: ${conversation.message[0].body}")
println("Message Two: ${conversation.message[1].body}")
}
}