title: Broadcast Receiver Long Binary SMS
body: <p>I have a problem when receiving a long <strong>binary</strong> SMS. The client is supposed to receive multipart binary SMS. Upon searching stackoverflow, people have mentioned that onReceive() shall be fired only once. From there you can concatenate the multiple SMS. </p>
  
  <p>I am not sure if that's the same when sending binary SMS. I receive 2 SMS, and onReceive is fired twice. The SMS are separate from each other making it impossible to concatenate.</p>
  
  <p><br/></p>
  
  <p>AndroidManifest:</p>
  
  <pre><code> &lt;uses-permission android:name="android.permission.READ_SMS" /&gt;
   &lt;uses-permission android:name="android.permission.SEND_SMS" /&gt;
   &lt;uses-permission android:name="android.permission.RECEIVE_SMS" /&gt;
   &lt;uses-permission android:name="android.permission.INTERNET" /&gt;
  
   &lt;receiver
          android:name="com.myapp.smsnotification.SMSReceiver"
          android:enabled="true" &gt;
          &lt;intent-filter android:priority="100" &gt;
              &lt;action android:name="android.intent.action.DATA_SMS_RECEIVED" /&gt;
              &lt;data
                  android:host="localhost"
                  android:port="9515"
                  android:scheme="sms" /&gt;
          &lt;/intent-filter&gt;
   &lt;/receiver&gt;
  </code></pre>
  
  <p>SMSReceiver.java</p>
  
  <p>in my onReceive()</p>
  
  <pre><code>Bundle bundle = intent.getExtras();
  if (bundle != null) {
      Object[] pdus = (Object[]) bundle.get("pdus");
      SmsMessage[] messages = new SmsMessage[pdus.length];
      for (int i = 0; i &lt; pdus.length; i++) {
          messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
      }
      try {
          if (messages.length &gt; -1) {
              ByteArrayOutputStream os = new ByteArrayOutputStream();
              for (int i = 0; i &lt; messages.length; i++) {
                  SmsMessage message = messages[i];
                  os.write(message.getUserData());
              }
              byte[] stream = os.toByteArray();
              // do something with stream
          }
      } catch (Exception e) {
          Log.e(TAG, e.getMessage());
      }
  }
  </code></pre>