0

在我的应用程序中实现 SMS 检索器同意 API 时,我收到此“接收广播意图时出错”。这是我的片段中的广播接收器代码:

private val 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
                        Log.d("sms", "in success")
                        val consentIntent = extras?.getParcelable<Intent>(SmsRetriever.EXTRA_CONSENT_INTENT)
                        try {
                            Log.d("sms", "in success 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 ...
                            Log.d("sms", "Exception")
                        }
                    }
                    CommonStatusCodes.TIMEOUT -> {
                        // Time out occurred, handle the error.
                        Log.d("sms", "Timeout")
                    }
                }
            }
        }
    }

首先,我正在启动侦听器,并且在侦听器成功时,我正在注册广播接收器

这是我的听众

private fun startSmsListener(){
        val task = context?.let { SmsRetriever.getClient(it).startSmsUserConsent(null /* or null */) }
        task?.addOnSuccessListener {
            showToast("Sms auto detection started")
            Log.d("sms", "Sms auto detection started")
            val intentFilter = IntentFilter(SmsRetriever.SMS_RETRIEVED_ACTION)
            context?.registerReceiver(smsVerificationReceiver, intentFilter)
        }
        task?.addOnFailureListener {
            showToast("Sms auto detection failed")
            Log.d("sms", "Sms auto detection failed")
        }
    }

最后我在 onActivityResult 中得到了结果

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
                    Log.d("OTP detected", message)
                    //txt_otp.setText(oneTimeCode)

                    // send one time code to the server
                } else {
                    // Consent denied. User can type OTC manually.
                }
        }
    }
4

0 回答 0