Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Create SMS from PDU deprecated API?

I want to create an app that gets notified when an SMS arrives and processes that SMS, but reading the reference for the createFromPdu function, it states that:

This method will soon be deprecated and all applications which handle incoming SMS messages by processing the SMS_RECEIVED_ACTION broadcast intent must now pass the new format String extra from the intent into the new method createFromPdu(byte[], String) which takes an extra format parameter. This is required in order to correctly decode the PDU on devices that require support for both 3GPP and 3GPP2 formats at the same time, such as dual-mode GSM/CDMA and CDMA/LTE phones.

However the createFromPdu(byte[], String) function is missing from both the documentation and the SDK.

What should I do? I need to support these dual-sim dual-mode phones.

like image 774
Zoltán Szőcs Avatar asked Sep 18 '13 15:09

Zoltán Szőcs


4 Answers

In short, use this:

SmsMessage smsMessage;

if (Build.VERSION.SDK_INT >= 19) { //KITKAT         
    SmsMessage[] msgs = Telephony.Sms.Intents.getMessagesFromIntent(intent);              
    smsMessage = msgs[0];            
} else {             
    Object pdus[] = (Object[]) bundle.get("pdus");             
    smsMessage = SmsMessage.createFromPdu((byte[]) pdus[0]);          
}
like image 78
Eran Katsav Avatar answered Sep 28 '22 06:09

Eran Katsav


    public void onReceive(Context context, Intent intent) {         SmsMessage[] msgs = Telephony.Sms.Intents.getMessagesFromIntent(intent);         String format = intent.getStringExtra("format");           SmsMessage sms = msgs[0];         Log.v("TAG", "handleSmsReceived" + (sms.isReplace() ? "(replace)" : "") +                 " messageUri: " +                 ", address: " + sms.getOriginatingAddress() +                 ", body: " + sms.getMessageBody());          String message = sms.getMessageBody(); } 

If you want to create SmsMessage object from pdu , you can use the new api

http://developer.android.com/reference/android/telephony/SmsMessage.html#createFromPdu(byte[], java.lang.String)

  SmsMessage sms2 = SmsMessage.createFromPdu(msgs[0].getPdu(),format); 
like image 31
AnhSirk Dasarp Avatar answered Sep 23 '22 21:09

AnhSirk Dasarp


You don't mention which Android version you're targeting but given the date of the question I'm assuming Jelly Bean 4.x.

At the time of writing this, we're at Lollipop MR1 and the deprecation note about using createFromPdu with the format parameter still stands:

https://android.googlesource.com/platform/frameworks/opt/telephony/+/android-5.1.0_r3/src/java/android/telephony/SmsMessage.java

Instead of using that API directly however, you can use the Telephony provider API getMessagesFromIntent: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.1.2_r1/android/provider/Telephony.java/#686

That method considers the PDU format (and the subscription ID in Lollipop for Multi SIM devices) and gives you an array of SmsMessage instances.

An example of how it's used can be seen in Google's MMS app:

https://android.googlesource.com/platform/packages/apps/Mms/+/master/src/com/android/mms/transaction/SmsReceiverService.java (see handleSmsReceived)

like image 2
fejd Avatar answered Sep 28 '22 06:09

fejd


createFromPdu(byte[]) is deprecated from SDK_INT 23 so you have to use

createFromPdu((byte[]) , format);

here is my code to get incoming SMS

i am using SMSReceiver class as an innerclass of an Activity.

this is my working code

public class SamplaActivity extends BaseActivity {
    SMSReceiver otp;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        otp=new SMSReceiver();


........
........
    }

  @Override
    protected void onResume() {
        super.onResume();

//        register broadcast receiver
        IntentFilter filter = new IntentFilter();
        filter.addAction("android.provider.Telephony.SMS_RECEIVED");
        registerReceiver(otp, filter);
    }
@Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(otp);
    }

    private class SMSReceiver extends BroadcastReceiver {
        private Bundle bundle;
        private SmsMessage currentSMS;
        private String message;

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

            if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {
                bundle = intent.getExtras();
                if (bundle != null) {
                    Object[] pdu_Objects = (Object[]) bundle.get("pdus");
                    if (pdu_Objects != null) {

                        for (Object aObject : pdu_Objects) {

                            currentSMS = getIncomingMessage(aObject, bundle);

                            String senderNo = currentSMS.getDisplayOriginatingAddress();

                            message = currentSMS.getDisplayMessageBody();
                            Toast.makeText(OtpActivity.this, "senderNum: " + senderNo + " :\n message: " + message, Toast.LENGTH_LONG).show();
                        }
                        this.abortBroadcast();
                        // End of loop
                    }
                }
            } // bundle null
        }
    }

private SmsMessage getIncomingMessage(Object aObject, Bundle bundle) {
    SmsMessage currentSMS;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        String format = bundle.getString("format");
        currentSMS = SmsMessage.createFromPdu((byte[]) aObject, format);
    } else {
        currentSMS = SmsMessage.createFromPdu((byte[]) aObject);
    }

    return currentSMS;
}

}
like image 2
sivaBE35 Avatar answered Sep 28 '22 07:09

sivaBE35