Android中如何自动读取短信?

rav*_*avi 3 android sms-retriever-api

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

\n\n

Google 推出了 SMS Retriever API,可以自动获取应用内通过 SMS 发送的验证码。

\n\n

但这些API都没有表达清楚,很混乱。不知道是不是我觉得很混乱。无论如何,这是我研究过的阅读短信的内容,但我什么也看不懂。

\n\n

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

\n\n

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

\n\n

我使用了这些依赖项

\n\n
implementation \'com.google.android.gms:play-services-auth:17.0.0\'\nimplementation \'com.google.android.gms:play-services-auth-api-phone:17.0.0\'\n
Run Code Online (Sandbox Code Playgroud)\n\n

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

\n\n

这是该教程的链接

\n\n

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

\n

Viv*_*hra 6

您应该使用 sms 检索器 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'
Run Code Online (Sandbox Code Playgroud)

在您的活动/片段中定义几个这样的变量

private val SMS_CONSENT_REQUEST = 2
private lateinit var smsVerificationReceiver: BroadcastReceiver
Run Code Online (Sandbox Code Playgroud)

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

 SmsRetriever.getClient(this).startSmsUserConsent(null)
 smsReceiver()
 val intentFilter = IntentFilter(SmsRetriever.SMS_RETRIEVED_ACTION)
 registerReceiver(smsVerificationReceiver, intentFilter)
Run Code Online (Sandbox Code Playgroud)

以下是广播接收器的方法

 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.
                    }
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

然后在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.
            }
    }
}
Run Code Online (Sandbox Code Playgroud)

另外不要忘记在 onDestroy() 方法中注销接收器

 unregisterReceiver(smsVerificationReceiver)
Run Code Online (Sandbox Code Playgroud)