2

从 2019 年 1 月 9 日起,如果 Google 没有说明必要性,则 Google 将从具有 READ SMS AND CALL LOG 权限的应用程序从 Playstore 中删除。

Google 引入了 SMS Retriever API 来自动获取应用内通过 SMS 发送的验证码。

但是这些 API 没有明确表达,非常混乱。不知道是不是我觉得很乱。无论如何,这是我为阅读短信而研究的内容,但我什么也听不懂。

我不确定这是否是自动阅读短信的正确链接。

https://developers.google.com/identity/sms-retriever/request

我使用了这些依赖项

implementation 'com.google.android.gms:play-services-auth:17.0.0'
implementation 'com.google.android.gms:play-services-auth-api-phone:17.0.0'

有一个很好的教程来实现自动阅读短信,但有些 API 已被弃用,所以我试图找到任何简单的解释来在 Android 中实现自动阅读短信。

这是该教程的链接

https://androidwave.com/automatic-sms-verification-android/

4

1 回答 1

5

您应该使用短信检索器 api 来阅读 otp 消息。这是您如何做到这一点的方法。

短信检索代码需要以下 2 个依赖项

implementation 'com.google.android.gms:play-services-auth:17.0.0'
implementation 'com.google.android.gms:play-services-auth-api-phone:17.1.0'

在您的活动/片段中定义一些这样的变量

private val SMS_CONSENT_REQUEST = 2
private lateinit var smsVerificationReceiver: BroadcastReceiver

在您的 onCreate() 方法中启动 SMS 检索器

 SmsRetriever.getClient(this).startSmsUserConsent(null)
 smsReceiver()
 val intentFilter = IntentFilter(SmsRetriever.SMS_RETRIEVED_ACTION)
 registerReceiver(smsVerificationReceiver, intentFilter)

下面是广播接收器的方法

 private fun smsReceiver() {
    smsVerificationReceiver = object : BroadcastReceiver() {
        override fun onReceive(context: Context, intent: Intent) {
            if (SmsRetriever.SMS_RETRIEVED_ACTION == intent.action) {
                val extras = intent.extras
                val smsRetrieverStatus = extras?.get(SmsRetriever.EXTRA_STATUS) as Status

                when (smsRetrieverStatus.statusCode) {
                    CommonStatusCodes.SUCCESS -> {
                        // Get consent intent
                        val consentIntent =
                            extras.getParcelable<Intent>(SmsRetriever.EXTRA_CONSENT_INTENT)
                        try {
                            // Start activity to show consent dialog to user, activity must be started in
                            // 5 minutes, otherwise you'll receive another TIMEOUT intent
                            startActivityForResult(consentIntent, SMS_CONSENT_REQUEST)
                        } catch (e: ActivityNotFoundException) {
                            // Handle the exception ...
                        }
                    }
                    CommonStatusCodes.TIMEOUT -> {
                        // Time out occurred, handle the error.
                    }
                }
            }
        }
    }
}

然后在 onActivityResult() 中就可以得到验证码

public override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    when (requestCode) {
        // ...
        SMS_CONSENT_REQUEST ->
            // Obtain the phone number from the result
            if (resultCode == Activity.RESULT_OK && data != null) {
                // Get SMS message content
                val message = data.getStringExtra(SmsRetriever.EXTRA_SMS_MESSAGE)
                // Extract one-time code from the message and complete verification
                // `message` contains the entire text of the SMS message, so you will need
                // to parse the string.
                val oneTimeCode = parseOneTimeCode(message) // define this function
                et_otp.setText(oneTimeCode.toString())
                // send one time code to the server
            } else {
                // Consent denied. User can type OTC manually.
            }
    }
}

也不要忘记在 onDestroy() 方法中取消注册接收器

 unregisterReceiver(smsVerificationReceiver)
于 2019-11-06T09:58:01.577 回答