1

I want to create app, that can receive SMS from port! but when run it, my msg.getMessageBody() returns null. How can I fixed it?

manifext.xml

<receiver android:name="data.SMSReceiver" android:enabled="true">
        <intent-filter>
            <action android:name="android.provider.telephony.SMS_RECEIVED" />
            <data android:scheme="sms"
                android:host="*"
                android:port="4030"/>
        </intent-filter>

Broadcast Receiver code

public class SMSReceiver extends BroadcastReceiver {

private static final String SHORTCODE = "+9810004473";

@Override
public void onReceive(Context context, Intent intent) {
    Bundle bundle = intent.getExtras();

    Object[] messages = (Object[])bundle.get("pdus");
    SmsMessage[] sms = new SmsMessage[messages.length];
    for(int n=0; n < messages.length; n++) {
        sms[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
    }
    for(SmsMessage msg : sms) {
        if(TextUtils.equals(msg.getOriginatingAddress(), SHORTCODE)) {
            Toast.makeText(context, "SMS: " + msg.getMessageBody(), Toast.LENGTH_LONG).show();
        }
    }
}

}

My Toast show: SMS: null

Nikhil
  • 3,711
  • 8
  • 32
  • 43
  • 1
    There won't be a message body on a data SMS message. Use the `SmsMessage#getUserData()` method to retrieve the data, which will be in the form of a `byte` array, as shown in the accepted answer on the linked post above. – Mike M. Sep 27 '16 at 06:52
  • tnx! that's right! – Iman Salehi Sep 27 '16 at 11:05

0 Answers0