首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何利用防火墙实现otp的自动检测?

如何利用防火墙实现otp的自动检测?
EN

Stack Overflow用户
提问于 2022-03-21 00:41:49
回答 1查看 213关注 0票数 0

我已经使用firebase实现了OTP验证,现在我想在tha中添加自动检测OTP验证功能。但是在自动检测之前,应用程序应该询问自动检测的权限。怎么做?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-03-21 03:26:00

这就是你要的

分度

代码语言:javascript
复制
    implementation 'com.google.android.gms:play-services-auth:20.1.0'
    implementation 'com.google.android.gms:play-services-auth-api-phone:18.0.1'
代码语言:javascript
复制
class SMSReceiver : BroadcastReceiver() {
    private var otpListener: OTPReceiveListener? = null

    fun setOTPListener(otpListener: OTPReceiveListener?) {
        this.otpListener = otpListener
    }

    override fun onReceive(context: Context, intent: Intent) {
        if (intent.action == SmsRetriever.SMS_RETRIEVED_ACTION) {
            val extras = intent.extras
            val status = extras!![SmsRetriever.EXTRA_STATUS] as Status?
            when (status!!.statusCode) {
                CommonStatusCodes.SUCCESS -> {
                    val sms = extras[SmsRetriever.EXTRA_SMS_MESSAGE] as String?
                    sms?.let {
                        val p = Pattern.compile("\\d+")
                        val m = p.matcher(it)
                        if (m.find()) {
                            val otp = m.group()
                            if (otpListener != null) {
                                otpListener!!.onOTPReceived(otp)
                            }
                        }
                    }
                }
            }
        }
    }

    interface OTPReceiveListener {
        fun onOTPReceived(otp: String?)
    }
}

清单

代码语言:javascript
复制
<receiver
   android:name=".SMSReceiver"
   android:exported="true"
   android:permission="com.google.android.gms.auth.api.phone.permission.SEND">
            <intent-filter>
                <action android:name="com.google.android.gms.auth.api.phone.SMS_RETRIEVED" />
            </intent-filter>
</receiver>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71551739

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档