Skip to content Skip to sidebar Skip to footer

Read Sms Only For Otp

In my android application i am implementing auto fill OTP from SMS, I learnt from this link http://androiddhina.blogspot.in/2015/06/reading-incoming-message-automatically-to-verify

Solution 1:

try this out.

privateBroadcastReceiverSmsListener=newBroadcastReceiver() {

        @SuppressWarnings("deprecation")@OverridepublicvoidonReceive(Context context, Intent intent) {
            if (intent.getAction().equals(
                    "android.provider.Telephony.SMS_RECEIVED")) {
                Bundlebundle= 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 = newSmsMessage[pdus.length];
                        for (inti=0; i < msgs.length; i++) {
                            msgs[i] = SmsMessage
                                    .createFromPdu((byte[]) pdus[i]);
                            // msg_from = msgs[i].getOriginatingAddress();StringmsgBody= msgs[i].getMessageBody();
                            // do your stuff
                        }
                    } catch (Exception e) {
                        // Log.d("Exception caught",e.getMessage());
                    }
                }
            }
        }
    };

and as per i did

@OverrideprotectedvoidonPause() {
    super.onPause();
    YourActivity.this.unregisterReceiver(SmsListener);
};

@OverrideprotectedvoidonResume() {
    super.onResume();
    IntentFilteri=newIntentFilter(
            "android.provider.Telephony.SMS_RECEIVED");
    YourActivity.this.registerReceiver(SmsListener, i);
}

but as others suggested you can register BroadcastReceiver after request for OTP and unregistered after getting OTP.

Happy Coding.

Solution 2:

your can enable and disable the receiver whenever you want. Try the following,

For Enabling the receiver

publicvoidenableSMSReceiver(Context context){
    ComponentNamecomponent=newComponentName(context, YOUR_RECEIVER.class);
    PackageManagerpm= context.getPackageManager();
    pm.setComponentEnabledSetting(
            component,
            PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
            PackageManager.DONT_KILL_APP);
}

For Disbling the receiver

publicstaticvoiddisableSMSReceiver(Context context){
ComponentNamecomponent=newComponentName(context, YOUR_RECEIVER.class);
PackageManagerpm= context.getPackageManager();
pm.setComponentEnabledSetting(
        component,
        PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
        PackageManager.DONT_KILL_APP);
}

Solution 3:

Now there are other options to read the OTP SMS automatically, where your app reads only your OTP SMS and this doesn't require any permission grants from the users.

1. Using SMS Retriever API in Google play services:

https://developers.google.com/identity/sms-retriever/overview

https://www.youtube.com/watch?v=jzWYv8y2v1c

But this requires some server level changes in the OTP SMS format. And this works only in the devices that have Play services installed.

2. Using createAppSpecificSmsToken in the SmsManager class (from Android O only):

https://developer.android.com/reference/android/telephony/SmsManager.html#createAppSpecificSmsToken(android.app.PendingIntent

https://code.tutsplus.com/tutorials/android-o-phone-number-verification-with-sms-token--cms-29141

This works only in Android O, as of now.

Post a Comment for "Read Sms Only For Otp"