如何将两个SmsMessages合并为一个SmsMessage?

时间:2015-08-19 07:42:59

标签: java android smsmanager

我使用以下代码创建广播:

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);

        }
    }

但是当我收到一个长短信(两个短信合二为一)时,对于前。短信发送:“bodyOfTheFirstSMS,bodyOfTheSecondSMS”,然后将其剪切为两个日志:

1) senderNum: +333333333333; message: "bodyOfTheFirstSMS,"; Date=1439968674000
2) senderNum: +333333333333; message: "bodyOfTheSecondSMS"; Date=1439968677000

不同之处仅在于此消息的日期。

如何检查它是单个短信的一部分? SmsMessage中是否有任何字段,表明这是一条消息?

1 个答案:

答案 0 :(得分:1)

您需要手动连接多部分消息。 在您的代码中,您需要进行一些修改以分别处理多部分消息,如下所示。

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) {

        }

    }
}