如何在安卓系统中自动读取短信?

3

从2019年1月9日起,如果应用程序没有解释其必要性,谷歌将从Playstore中删除具有读取短信和通话记录权限的应用程序。

谷歌推出了SMS Retriever API,可以自动获取应用程序内通过短信发送的验证代码。

但是这些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/

1个回答

6

您应该使用SMS Retriever API 来读取OTP消息。下面是您可以执行此操作的方法。

您需要以下两个依赖项以获取短信检索代码。

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

在您的activity/fragment中定义几个变量,像这样:
private val SMS_CONSENT_REQUEST = 2
private lateinit var smsVerificationReceiver: BroadcastReceiver

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

 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)

更多详细信息,请查看谷歌文档。 https://developers.google.com/identity/sms-retriever/user-consent/request - Morgan Koh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接