Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - receiving long SMS (multipart)

Tags:

android

sms

I have an application, which has to listen for specific SMS. So far easy.

But when I receive the message, it's multipart. Is there a proper way to receive the SMS as one message?

Now my activity starts two times, for each part of the sms. Should I concatenate the SMS by hand?

like image 300
Danail Avatar asked Nov 29 '10 16:11

Danail


3 Answers

It may be useful to look at how gTalkSMS handles incoming SMS'es, as it appears to handle multipart messages correctly.

like image 87
Sebastian Paaske Tørholm Avatar answered Sep 27 '22 18:09

Sebastian Paaske Tørholm


Bundle bundle  = intent.getExtras(); Object[] pdus = (Object[]) bundle.get("pdus");             messages = new SmsMessage[pdus.length];             for (int i = 0; i < pdus.length; i++)             {                 messages[i] =                     SmsMessage.createFromPdu((byte[]) pdus[i]);             }  SmsMessage sms = messages[0]; try {   if (messages.length == 1 || sms.isReplace()) {     body = sms.getDisplayMessageBody();   } else {     StringBuilder bodyText = new StringBuilder();     for (int i = 0; i < messages.length; i++) {       bodyText.append(messages[i].getMessageBody());     }     body = bodyText.toString();   } } catch (Exception e) {  } 
like image 34
om252345 Avatar answered Sep 27 '22 17:09

om252345


Shorter solution:

if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {
                    Bundle bundle = intent.getExtras();           //---get the SMS message passed in---
                    SmsMessage[] msgs = null;

                    if (bundle != null) {
                        //---retrieve the SMS message received---
                        try {
                            Object[] pdus = (Object[]) bundle.get("pdus");
                            msgs = new SmsMessage[pdus.length];
                            String msgBody = "";
                            String msg_from = "";
                            for (int i = 0; i < msgs.length; i++) {
                                msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
                                msg_from = msgs[i].getOriginatingAddress();
                                msgBody += msgs[i].getMessageBody();
                            }

                        } catch (Exception e) {
    //                            Log.d("Exception caught",e.getMessage());
                        }
                    }
                }
like image 23
M. Usman Khan Avatar answered Sep 27 '22 17:09

M. Usman Khan