Created
December 23, 2020 05:00
-
-
Save shikto1/cbfef59ab57a66afdd936bf86fe94c5c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MainActivity : AppCompatActivity() { | |
private var intentFilter: IntentFilter? = null | |
private var smsReceiver: SMSReceiver? = null | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
// Init Sms Retriever >>>> | |
initSmsListener() | |
initBroadCast() | |
} | |
private fun initBroadCast() { | |
intentFilter = IntentFilter(SmsRetriever.SMS_RETRIEVED_ACTION) | |
smsReceiver = SMSReceiver() | |
smsReceiver?.setOTPListener(object : OTPReceiveListener { | |
override fun onOTPReceived(otp: String?) { | |
showToast("OTP Received: $otp") | |
} | |
}) | |
} | |
private fun initSmsListener() { | |
val client = SmsRetriever.getClient(this) | |
client.startSmsRetriever() | |
} | |
override fun onResume() { | |
super.onResume() | |
registerReceiver(smsReceiver, intentFilter) | |
} | |
private fun showToast(msg: String?) { | |
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show() | |
} | |
fun onPause() { | |
super.onPause() | |
unregisterReceiver(smsReceiver) | |
} | |
override fun onDestroy() { | |
super.onDestroy() | |
smsReceiver = null | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment