Search code examples
smsbroadcastreceivertelephony

Telephony class error


I am learning from a tutorial, and the following code doesn't work:

SmsMessage msg[] = Telephony.Sms.Intents.getMessagesFromIntent(intent);

I think it is no more part of the framework, but I haven't found what to use instead.

How can I get the sms message? (this is in the onReceive method of a BroadcastReceiver)


Solution

  • I found a solution :)

    Bundle bundle = intent.getExtras();
        SmsMessage[] msgs = null;
        String str = "";
        if (bundle != null) {
            // ---retrieve the SMS message received---
            Object[] pdus = (Object[]) bundle.get("pdus");
            msgs = new SmsMessage[pdus.length];
            for (int i = 0; i < msgs.length; i++) {
                msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
    
                str += msgs[i].getMessageBody().toString();
    
            }