Search code examples
androidandroid-broadcastreceiversmsmanager

How to handle sms with more than 160 characters like 250 characters in broadcast receiver


How can i handle multipart sms in broadcast receiver and store it in one string before start operation on it like i want to store multipart sms in one string and then split it with different delimiter and then get status codes from it i have 1 sms with 250 characters . i tried with below code but not working i tested it on emulator kindly help me i also put code for receiving.

String mySmsText ;
        public void onReceive(Context context, Intent intent) 
        {
            Bundle bundle = intent.getExtras();
            if (bundle != null) {
             Object[] pdus = (Object[])bundle.get("pdus");
             final SmsMessage[] messages = new SmsMessage[pdus.length];
             for (int i = 0; i < pdus.length; i++) {
                messages[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
              }
                 StringBuffer content = new StringBuffer();
                  if (messages.length > 0) {
                  for (int i = 0; i < messages.length; i++) {
                      content.append(messages[i].getMessageBody());
                        }
                    }
                   mySmsText = content.toString();
                  }    
            instance.t1.setText(mySmsText);
        }
   } 

i tried to print msg but it display bad characters also overrides old one.

I'm sorry in first there is no problem with my code the problem is only with emulator on which i was tested. when i test it on real device it work well i'm sorry ...


Solution

  • Finally i done it thanx mr AxelH for your answer

    DataBaseHandler db;
            String mySmsText;
            public void onReceive(Context context, Intent intent) {
                db = new DataBaseHandler(context);
                Bundle bundle = intent.getExtras();
                if (bundle != null) {
                    Object[] pdus = (Object[]) bundle.get("pdus");
                    final SmsMessage[] messages = new SmsMessage[pdus.length];
                    for (int i = 0; i < pdus.length; i++)
                        messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
                    if (messages.length > 0) {
                        StringBuffer content = new StringBuffer();
                        for (SmsMessage sms : messages)
                            content.append(sms.getDisplayMessageBody());
                        mySmsText = content.toString();
                    }
                    db.update_sys_pwd(mySmsText);
                    instance.t1.setText(mySmsText);
                }
            }