My receiver, it works fine when short sms arrive, but at large, it intercepts with chunks.

@Override public void onReceive(Context context, Intent intent) { Log.d(LOG_TAG, "onReceive()"); Bundle bundle = intent.getExtras(); SmsMessage[] msgs = null; SmsMessage[] phonenum = null;// if (bundle != null) { // ---retrieve the SMS message received--- Object[] pdus = (Object[]) bundle.get("pdus"); msgs = new SmsMessage[pdus.length]; for (int i = 0; i < msgs.length; i++) { msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]); body += msgs[i].getMessageBody().toString(); } // ---retrieve the SMS senders number --- phonenum = new SmsMessage[pdus.length]; for (int i = 0; i < phonenum.length; i++) { phonenum[i] = SmsMessage.createFromPdu((byte[]) pdus[i]); PhoneNUMBER += phonenum[i].getOriginatingAddress(); } 

It turns out that if a large message comes in, consisting of 2 or 3 sms, then it does not take it completely, but places it in the body variable, only the first part, and the rest does not process at all. It turns out the user sees the first SMS, does not see the whole picture, and the rest of the SMS come in the usual way, through a notification. So I wanted to know how to intercept such messages?

    2 answers 2

    Keep a piece of code to intercept multipart sms. Returns Map<String address, String text> :

     private Map<String, String> retrieveMessages(Intent intent) { Map<String, String> msg = null; SmsMessage[] msgs = null; 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].getOriginatingAddress(); // 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].getMessageBody()); } 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; } 

    Update

    Call:

     Map<String, String> msgs = retrieveMessages(intent); for (String address : msgs.keySet()) String msg = msgs.get(address); //сообщение от адресата 
    • Many thanks, of course, but for me the hash card is still complicated in understanding. Do not think for arrogance, how to use this method? I need to create a hash map, Map staff = new HashMap <String, Employee> (); call the method staff = retrieveMessages (intent); from the onReceive method, it will return the card; you must assign it to the hash card, right? but how to get the string values ​​from the card? Will this method work? for (Map.Entry entry: staff.entrySet ()) {String key = entry.getKey (); Employee value = entry.getValue (); } - FFFNikolay
    • one
      @FFFNikolay how can you try to write something without understanding how the most primitive Java collection works? See the update. - Barmaley
    • one
      Well, not Vasiliev is for sure. Shield is not bad, but he himself did not succeed. Previously, there was a book by Peter Norton about Java - (long true). I think this is the best book - it will probably be difficult to find now. So read Shilda. - Barmaley
    • one
      It is difficult to advise a beginner. See Horstmann (a two-volume Java 2). For my taste, everything is somewhat prolonged, but I liked it in principle. - avp
    • one
      @FFFNikolay address already comes in the array of keys Map<String, String> - well, a bad head does not give rest to his hands? - Barmaley

    Try this

     String message; 

    ...

      //---retrieve the SMS message received--- Object[] pdus = (Object[]) bundle.get("pdus"); SmsMessage[] messages=new SmsMessage[pdus.length]; for(int i=0;i<pdus.length;i++) { messages[i]=SmsMessage.createFromPdu((byte[]) pdus[i]); } message = getMessageFromSMSs( messages); 

    ....

     private String getMessageFromSMSs(SmsMessage[] sms) { String str=""; for(SmsMessage message:sms) { str += message.getMessageBody().toString(); } return str; } 

    ...

      String phoneNumber=messages[0].getOriginatingAddress(); 
    • Thank. Checked works better than mine (even though the entire message processes), but if a large delay turns out to be around 15-25 seconds, it breaks into two, cannot this be fixed? - FFFNikolay

    Protected by spirit community member Jan 11 '17 at 16:43 .

    Thank you for your interest in this issue. Since he collected a large number of low-quality and spam responses, which had to be deleted, now it’s necessary to have 10 reputation points on the site (the bonus for account association is not counted ).

    Maybe you want to answer one of the unanswered questions ?