3

We are looking to build the functionality in our app to read a security code that being sent as part of SMS and display on the textView. Also, I am not looking to build a broadcast receiver, may be an intent service which only will start run on a particular screen and kill the service once user navigated to another screen.

It would be great if anyone can shade some light and help with some sample code.

mdDroid
  • 3,135
  • 2
  • 22
  • 34
Bulu
  • 1,351
  • 3
  • 16
  • 37
  • you should check [Android – Listen For Incoming SMS Messages](http://stackoverflow.com/questions/7089313/android-listen-for-incoming-sms-messages) – Ravi Jun 10 '16 at 04:18
  • Did you even try searching? There are literally hundreds of posts on this site alone that deal with handling SMS in Android. – Mike M. Jun 10 '16 at 04:26
  • If the security code is your only concern, you could use a delimiter before and/or after your security code and split the `String` in the SMS using that delimiter. This would give you your security code. If you want to know how to intercept incoming messages, you could look at @RRR's link or [this link](http://www.androidhive.info/2015/08/android-adding-sms-verification-like-whatsapp-part-2/). – Ishita Sinha Jun 10 '16 at 04:26
  • 1
    Possible duplicate of [How can I read SMS messages from the inbox programmatically in Android?](http://stackoverflow.com/questions/848728/how-can-i-read-sms-messages-from-the-inbox-programmatically-in-android) – Janki Gadhiya Jun 10 '16 at 05:15

1 Answers1

5

To read incoming SMS you have to do three things.

  1. Broadcast Receiver
  2. Declare Broadcast Receiver in manifest
  3. Need SMS Receive permissions

Note: If you are compiling against 6.0 Marshmallow you have get android.permission.RECEIVE_SMS at runtime. Runtime Permissions

Lets Starts Receiving incoming SMS

1) First add permissions in manifest

<uses-permission android:name="android.permission.RECEIVE_SMS" />

2) Declare Broadcast Receiver in Manifest.

What this declaration do it will inform you when ever a new SMS Receive by device.

<receiver android:name="com.example.abc.ReciveSMS">
    <intent-filter>
        <action android:name="android.provider.Telephony.SMS_RECEIVED" />
    </intent-filter>
</receiver>

3) Add this code to your declared class in manifest

public class ReciveSMS extends BroadcastReceiver{

    private SharedPreferences preferences;

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub

        if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){
            Bundle bundle = intent.getExtras();           //---get the SMS message passed in---
            SmsMessage[] msgs = null;
            String msg_from;
            if (bundle != null){
                //---retrieve the SMS message received---
                try{
                    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]);
                        msg_from = msgs[i].getOriginatingAddress();
                        String msgBody = msgs[i].getMessageBody();
                    }
                }catch(Exception e){
//                            Log.d("Exception caught",e.getMessage());
                }
            }
        }
    }
}

Original Post here.

Community
  • 1
  • 1
Sohail Zahid
  • 8,099
  • 2
  • 25
  • 41
  • 1
    Sohail thanks for answering my question. My question is around the broadcast receiver. I would like to run a background service here as the service should start only when user is on a particular screen then need to kill the service. Broadcast receiver is long running background task – Bulu Jun 10 '16 at 05:21
  • 1
    ok no problem you can register a receiver too for specific screen when jump to that screen it will recive sms when you jump out from that screen you when will not receive sms...is this work for you? – Sohail Zahid Jun 10 '16 at 05:27
  • I have 2 sims in my phone! Is it possible to check which sim is receiving message? – Huzaifa Asif Oct 03 '19 at 07:06