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-OTP.html
My doubt is BroadcastReceiver
reads every sms received by the user, can we stop this? I need to read sms at a particular time Example: only after user clicks the send OTP button after that I should stop reading sms.
Please help me how to start and stop the BroadcastReceiver
for particular period of time.
try this out.
private BroadcastReceiver SmsListener = new BroadcastReceiver() {
@SuppressWarnings("deprecation")
@Override
public void onReceive(Context context, Intent intent) {
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();
// do your stuff
}
} catch (Exception e) {
// Log.d("Exception caught",e.getMessage());
}
}
}
}
};
and as per i did
@Override
protected void onPause() {
super.onPause();
YourActivity.this.unregisterReceiver(SmsListener);
};
@Override
protected void onResume() {
super.onResume();
IntentFilter i = new IntentFilter(
"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.
your can enable and disable the receiver whenever you want. Try the following,
For Enabling the receiver
public void enableSMSReceiver(Context context){
ComponentName component = new ComponentName(context, YOUR_RECEIVER.class);
PackageManager pm = context.getPackageManager();
pm.setComponentEnabledSetting(
component,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
}
For Disbling the receiver
public static void disableSMSReceiver(Context context){
ComponentName component = new ComponentName(context, YOUR_RECEIVER.class);
PackageManager pm = context.getPackageManager();
pm.setComponentEnabledSetting(
component,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
}
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.