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.
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.
your can enable and disable the receiver whenever you want. Try the following,
For Enabling the receiver
For Disbling the receiver
try this out.
and as per i did
but as others suggested you can register BroadcastReceiver after request for OTP and unregistered after getting OTP.
Happy Coding.