为什么`SmsMessage.createFromPdu`返回一个数组?(Why does `SmsMessage.createFromPdu` return an array?)

编程入门 行业动态 更新时间:2024-10-25 02:19:24
为什么`SmsMessage.createFromPdu`返回一个数组?(Why does `SmsMessage.createFromPdu` return an array?)

在Android中,如果我想读取传入的SMS,我会使用SmsMessage.createFromPdu ,但这会返回一个SmsMessage数组。 这是为什么? 为什么不只是一个SmsMessage ? 是因为长消息可以分为几个? 如果是这样,这是否意味着我可以指望所有这些SmsMessage具有相同的起始地址?

In Android, if I want to read an incoming SMS, I would use SmsMessage.createFromPdu, but this returns an array of SmsMessages. Why is that? Why not just a single SmsMessage? Is it because long messages could be divided into several? If so, does that mean I can count on all these SmsMessages to have the same originating address?

最满意答案

经过大量的研究,这是交易: 是的,您获得的这些消息是较大消息的细分。 SmsMessage数组包含可能相互关联或不相关的消息(不同的发件人)。 Android为何会将它们混合起来? 我不知道。 您应该始终循环遍历它们并通过SmsMessage.getDisplayOriginatingAddress()它们进行分组。 然后,对于每组消息,从SmsMessage.getDisplayMessageBody()追加它们的主体以重建更大的消息。 以下是GTalk应用程序源代码示例(感谢@hungryghost):

private static Map<String, String> RetrieveMessages(Intent intent) { Map<String, String> msg = null; SmsMessage[] msgs; Bundle bundle = intent.getExtras(); if (bundle != null && bundle.containsKey("pdus")) { Object[] pdus = (Object[]) bundle.get("pdus"); if (pdus != null) { int nbrOfpdus = pdus.length; msg = new HashMap<String, String>(nbrOfpdus); msgs = new SmsMessage[nbrOfpdus]; // There can be multiple SMS from multiple senders, there can be a maximum of nbrOfpdus different senders // However, send long SMS of same sender in one message for (int i = 0; i < nbrOfpdus; i++) { msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]); String originatinAddress = msgs[i].getDisplayOriginatingAddress(); // Check if index with number exists if (!msg.containsKey(originatinAddress)) { // Index with number doesn't exist // Save string into associative array with sender number as index msg.put(msgs[i].getOriginatingAddress(), msgs[i].getDisplayMessageBody()); } else { // Number has been there, add content but consider that // msg.get(originatinAddress) already contains sms:sndrNbr:previousparts of SMS, // so just add the part of the current PDU String previousparts = msg.get(originatinAddress); String msgString = previousparts + msgs[i].getMessageBody(); msg.put(originatinAddress, msgString); } } } } return msg; }

After doing tons of research, here's the deal: Yes, these messages that you get are the broken down pieces of a larger message. The array of SmsMessages contains messages that may or may not be related to each other (different senders). Why Android mixes them up like that? I don't know. You should always loop through them and group them by SmsMessage.getDisplayOriginatingAddress(). Then, for each group of messages, append their bodies from SmsMessage.getDisplayMessageBody() to reconstruct the larger message. Here's an example from the GTalk app source (thanks @hungryghost):

private static Map<String, String> RetrieveMessages(Intent intent) { Map<String, String> msg = null; SmsMessage[] msgs; Bundle bundle = intent.getExtras(); if (bundle != null && bundle.containsKey("pdus")) { Object[] pdus = (Object[]) bundle.get("pdus"); if (pdus != null) { int nbrOfpdus = pdus.length; msg = new HashMap<String, String>(nbrOfpdus); msgs = new SmsMessage[nbrOfpdus]; // There can be multiple SMS from multiple senders, there can be a maximum of nbrOfpdus different senders // However, send long SMS of same sender in one message for (int i = 0; i < nbrOfpdus; i++) { msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]); String originatinAddress = msgs[i].getDisplayOriginatingAddress(); // Check if index with number exists if (!msg.containsKey(originatinAddress)) { // Index with number doesn't exist // Save string into associative array with sender number as index msg.put(msgs[i].getOriginatingAddress(), msgs[i].getDisplayMessageBody()); } else { // Number has been there, add content but consider that // msg.get(originatinAddress) already contains sms:sndrNbr:previousparts of SMS, // so just add the part of the current PDU String previousparts = msg.get(originatinAddress); String msgString = previousparts + msgs[i].getMessageBody(); msg.put(originatinAddress, msgString); } } } } return msg; }

更多推荐

本文发布于:2023-08-03 14:18:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1392842.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   createFromPdu   SmsMessage   return   array

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!