2

我正在尝试在华为设备中检索 OTP。但它不工作。

我已经在华为开发者控制台中创建了具有所有要求的应用程序。

下面是我用来检索短信的代码。

private fun initSmsManager() {

        val task = ReadSmsManager.start(this@MainActivity)

        task.addOnCompleteListener {

            if (task.isSuccessful) {
                // The service is enabled successfully. Continue with the process.
                Toast.makeText(this, "ReadSms service has been enabled.", Toast.LENGTH_LONG).show()
            } else
                Toast.makeText(this, "The service failed to be enabled.", Toast.LENGTH_LONG).show()
        }
        task.addOnSuccessListener(this, OnSuccessListener {
                if(task.isSuccessful){
                    Toast.makeText(this, "ReadSms service has been enabled.", Toast.LENGTH_LONG).show()
                    myReceiver = MyBroadcastReceiver();
                    val intentFilter = IntentFilter(READ_SMS_BROADCAST_ACTION)
                    registerReceiver(myReceiver, intentFilter)
                }
        })
        task.addOnFailureListener(this, OnFailureListener {
            Toast.makeText(this,it.message,Toast.LENGTH_SHORT).show();
        })
    }

广播接收器

class MyBroadcastReceiver : BroadcastReceiver() {

    companion object {
        val TAG = MyBroadcastReceiver::class.java.simpleName
    }

    override fun onReceive(context: Context?, intent: Intent?) {

        val bundle = intent!!.extras
        if (bundle != null) {
            val status: Status? = bundle.getParcelable(ReadSmsConstant.EXTRA_STATUS)
            if (status?.statusCode == CommonStatusCodes.TIMEOUT) {

                // Service has timed out and no SMS message that meets the requirement is read. Service ended.
                // doSomethingWhenTimeOut()
            } else if (status?.statusCode == CommonStatusCodes.SUCCESS) {

                if (bundle.containsKey(ReadSmsConstant.EXTRA_SMS_MESSAGE)) {

                    // An SMS message that meets the requirement is read. Service ended.
                    //doSomethingWhenGetMessage(bundle.getString(ReadSmsConstant.EXTRA_SMS_MESSAGE))

                    bundle.getString(ReadSmsConstant.EXTRA_SMS_MESSAGE)?.let {

                        Log.d(TAG, it)

                        val local = Intent()
                        local.action = "service.to.activity.transfer"
                        local.putExtra("sms", it)
                        context!!.sendBroadcast(local)
                    }
                }
            }
        }
    }
}

在这方面的任何帮助都是有益的。

4

2 回答 2

1

请确认以下几点:

  1. 检查是否开启了接收短信验证码的广播。您可以通过执行断点调试或记录日志来做到这一点。
  2. 检查短信格式是否符合短信自动阅读规则。

有关详细信息,请参阅文档

在此处输入图像描述

检查hash_value字段是否正确。

  1. 如果在前面的检查过程中没有出现错误,请您提供完整的日志跟踪,然后我会尝试找出这个问题可能有什么问题。:)
于 2021-01-22T02:53:57.643 回答
0

所有代码都运行良好,当我构建发布的 apk 然后密钥库更改时,它在调试模式下运行良好,并且发布模式的哈希更改也发生了变化。如果您知道如何在发布模式下运行,请告诉我们。可能是华为开发者账号发生了一些变化

于 2021-01-25T08:20:47.173 回答