android:sms广播接收器没有获得更大的短信的全文

时间:2014-04-30 07:41:14

标签: android broadcastreceiver

似乎如果带有一些较大文本的短信出现,由于某种原因它被删除,所以不是整个短信进来,这是我的代码:

public class SmsReceiver extends BroadcastReceiver {
   // vars here

    @Override
    public void onReceive(Context context, Intent intent) {

        // Get SMS map from Intent
        Bundle extras = intent.getExtras();

        if (extras != null) {
            // Get received SMS array
            Object[] smsExtra = (Object[]) extras.get(SMS_EXTRA_NAME);

            String address = "";
            String body = "";

            for (int i = 0; i < smsExtra.length; ++i) {
                SmsMessage sms = SmsMessage.createFromPdu((byte[]) smsExtra[i]);
                body = sms.getMessageBody().toString();
                address = sms.getOriginatingAddress();
            }

      // show the popup
      Intent intnt = new Intent(context, ShowNotification.class);
      intnt.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      intnt.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
      intnt.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

      intnt.putExtra("address", address);
      intnt.putExtra("body", body);
      context.startActivity(intnt);

        }
    }
}

现在让我们假设短信是:

Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra.

我在toast中显示从上面的广播接收器收到的短信,只有最后几个字在吐司中显示,或者即使我将这个sms主体传递给另一个弹出意图,只有尽可能多的文本传递,如吐司所示: / p>

int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context,  body, duration);
toast.show();

因此,出于某种原因,不会有整个短信文本出现:(

谁能说出如何处理这个问题?正如我所看到的其他短信应用程序,如SMS Popup或Go SMS Pro,他们确实获得完整的短信文本,并在弹出或对话中显示。

感谢您的帮助

2 个答案:

答案 0 :(得分:8)

好的,所以我想出来了,我使用的代码只是最后一个短信,只需要附加所有短信:

此:

body = sms.getMessageBody().toString();

应该是:

body += sms.getMessageBody().toString();

答案 1 :(得分:0)

可能会帮助!

            String sender = "";
            StringBuilder messageBody= new StringBuilder();

            for (Object pdu : pdus) {
                SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) pdu);
                messageBody.append( smsMessage.getMessageBody());
                sender = smsMessage.getDisplayOriginatingAddress();
            }

            if (BuildConfig.DEBUG){
                Log.i("OKK", "SMS Body: " + messageBody.toString());
            }