Question

This is my code


public class SmsReceiver extends BroadcastReceiver {

    private static final String PDUS = "pdus";

    @Override
    public void onReceive(Context context, Intent intent) {
        Object[] pdus = (Object[]) bundle.get(PDUS);
        return SmsMessage.createFromPdu((byte[]) pdus[0]);
    }

}

Source text of sms:

even worse, mailparser seems to take a very simple single-part test email i have, but in addition to the 1.1, text/plain

Text that I get by calling methods message.getMessageBody() or getDisplayMessageBody():

even worse, mailparser seems to take a very simple single-part test

what am I doing wrong?

P.S. Hangouts returns full messages

Was it helpful?

Solution

Try this

public void onReceive(Context context, Intent intent) {
    Bundle extras = intent.getExtras();
    String sender = "", receivedMessage = "";
    String MSG_TYPE = intent.getAction();
    if (MSG_TYPE.equals("android.provider.Telephony.SMS_RECEIVED")) {
        Object[] pdus = (Object[]) extras.get("pdus");

        for (int i = 0; i < pdus.length; i++) {
            SmsMessage SMessage = SmsMessage
                    .createFromPdu((byte[]) pdus[i]);
            sender = SMessage.getOriginatingAddress();

            receivedMessage += SMessage.getMessageBody().toString();

        }

    }
}

OTHER TIPS

SMS is typically composed of many pdus, so you need code like this:

retMsgs = new SmsMessage[pdus.length];
for(int n=0; n < pdus.length; n++) {
    byte[] byteData = (byte[])pdus[n];
    retMsgs[n] = SmsMessage.createFromPdu(byteData);
}   

and then return the array.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top