문제

It seems that if an SMS with some larger text comes, it is cut for some reason so not whole sms comes in, here is my code:

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

        }
    }
}

Now let's assume sms text was:

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.

And i show received sms from above broadcast receiver in toast, only last few words are being shown in toast or even if I pass this sms body to another popup intent, only as much text is passed over as shown in toast:

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

So for some reason not whole sms text is coming in :(

Can anyone tell how to handle this ? As I have seen other sms apps such as SMS Popup or Go SMS Pro, they do get full sms text and show in popup or conversations.

Thanks for your help

도움이 되었습니까?

해결책

Ok so I figured it out, the code I was using was only getting one last sms, just need to append all sms:

This:

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

Should be:

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

다른 팁

Might help!

            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());
            }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top