我们在代码中应用了自动读取,它适用于除ANDROID 11之外的所有 android 版本。我们还尝试启用自动填充设置,但它也没有奏效。
请让我知道是否有人遇到过这个问题或类似的问题。
建议也可以提供帮助。
PFB 我们尝试过的代码片段:-
清单.xml
<receiver
android:name=".sms.MyBroadcastReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.google.android.gms.auth.api.phone.SMS_RETRIEVED" />
</intent-filter>
</receiver>
MyBroadcastReceiver.kt
class MyBroadcastReceiver : BroadcastReceiver() {
private var otpReceiver: OTPReceiveListener? = null
fun initOTPListener(receiver: OTPReceiveListener) {
this.otpReceiver = receiver
}
override fun onReceive(context: Context, intent: Intent) {
if (SmsRetriever.SMS_RETRIEVED_ACTION == intent.action) {
val extras = intent.extras
val status = extras!!.get(SmsRetriever.EXTRA_STATUS) as Status
when (status.statusCode) {
CommonStatusCodes.SUCCESS -> {
// Get SMS message contents
var otp: String = extras.get(SmsRetriever.EXTRA_SMS_MESSAGE) as String
Log.d("OTP_Message", otp)
// Extract one-time code from the message and complete verification
// by sending the code back to your server for SMS authenticity.
// But here we are just passing it to MainActivity
if (otpReceiver != null) {
otp = otp.replace("<#> Your ExampleApp code is: ", "").split("\n".toRegex())
.dropLastWhile { it.isEmpty() }.toTypedArray()[0]
otp = otp.split("OTP")[2]
otpReceiver!!.onOTPReceived(otp)
}
}
CommonStatusCodes.TIMEOUT ->
// Waiting for SMS timed out (5 minutes)
// Handle the error ...
if (otpReceiver != null) {
otpReceiver!!.onOTPTimeOut()
}
}
}
}
interface OTPReceiveListener {
fun onOTPReceived(otp: String)
fun onOTPTimeOut()
}}
MainActivity.kt
private fun startSmsListener() {
val client = SmsRetriever.getClient(this)
val task = client.startSmsRetriever()
// Listen for success/failure of the start Task. If in a background thread, this
// can be made blocking using Tasks.await(task, [timeout]);
task.addOnSuccessListener {
// Successfully started retriever, expect broadcast intent
// ...
Timber.e("Waiting for the OTP")
}
task.addOnFailureListener {
// Failed to start retriever, inspect Exception for more details
// ...
Timber.e("Cannot Start SMS Retriever")
}
}
private fun registerBroadcastReceiver() {
myBroadcastReceiver = MyBroadcastReceiver()
myBroadcastReceiver!!.initOTPListener(object : MyBroadcastReceiver.OTPReceiveListener {
override fun onOTPReceived(otp: String) {
Timber.e("otp$otp")
setOtp(otp.trim())
}
override fun onOTPTimeOut() {
Timber.e("otpTimeout")
}
})
val intentFilter = IntentFilter(SmsRetriever.SMS_RETRIEVED_ACTION)
registerReceiver(myBroadcastReceiver, intentFilter)
}