I create broadcast with following code (from this article):
public class IncomingSms extends BroadcastReceiver {
// Get the object of SmsManager
final SmsManager sms = SmsManager.getDefault();
public void onReceive(Context context, Intent intent) {
// Retrieves a map of extended data from the intent.
final Bundle bundle = intent.getExtras();
try {
if (bundle != null) {
final Object[] pdusObj = (Object[]) bundle.get("pdus");
for (int i = 0; i < pdusObj.length; i++) {
SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
String phoneNumber = currentMessage.getDisplayOriginatingAddress();
String senderNum = phoneNumber;
String message = currentMessage.getMessageBody();
Log.i("SmsReceiver", "senderNum: " + senderNum + "; message: " + message+"; Date="+currentMessage.getTimestampMillis());
//sending to API
} // end for loop
} // bundle is null
} catch (Exception e) {
Log.e("SmsReceiver", "Exception smsReceiver" +e);
}
}
But when I receive a long sms (two sms in one),for ex. SMS sent:"bodyOfTheFirstSMS,bodyOfTheSecondSMS", then it is cut to two logs:
1) senderNum: +333333333333; message: "bodyOfTheFirstSMS,"; Date=1439968674000
2) senderNum: +333333333333; message: "bodyOfTheSecondSMS"; Date=1439968677000
different is only Date of this messages.
How to check that it's a parts of the one single SMS? Is there any field in SmsMessage, that indicates that this is one message?
You will need to concatenate multi part message by hand. In your code you will need to do some modification to handle multipart messages separately as below.
public class IncomingSms extends BroadcastReceiver {
// Get the object of SmsManager
final SmsManager sms = SmsManager.getDefault();
public void onReceive(Context context, Intent intent) {
// Retrieves a map of extended data from the intent.
final Bundle bundle = intent.getExtras();
SmsMessage messages = null;
try {
if (bundle != null) {
final Object[] pdusObj = (Object[]) bundle.get("pdus");
messages = new SmsMessage[pdus.length];
for (int i = 0; i < pdusObj.length; i++) {
messages[i] = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
} // end for loop
} // bundle is null
} catch (Exception e) {
Log.e("SmsReceiver", "Exception smsReceiver" +e);
}
SmsMessage sms = messages[0];
String message;
try {
if (messages.length == 1 || sms.isReplace()) {
String phoneNumber = sms.getDisplayOriginatingAddress();
String senderNum = phoneNumber;
message = sms.getMessageBody();
Log.info("SmsReceiver", "senderNum: " + senderNum + "; message: " + message+"; Date="+sms.getTimestampMillis());
//sending to API
} else {
StringBuilder bodyText = new StringBuilder();
for (int j = 0; j < messages.length; j++) {
bodyText.append(messages[j].getMessageBody());
}
message = bodyText.toString();
}
} catch (Exception e) {
}
}
}