如何使SMS Retriever API侦听多个短信

时间:2019-02-01 12:05:19

标签: android google-play-services

SMS Retriever API一次不会监听多个短信。 有什么办法可以实现? 实际上,屏幕上有一个重试OTP按钮。 因此,在某些最坏的情况下,需要多次收听SMS。

我正在关注this tutorial

活动:

goNew = () => {
    this.goClose()
      Navigation.push('mainStack', {
        component: {
          name: 'NewComp',
          passProps: {
            text: 'Pushed screen'
          },
          options: {
            topBar: {
              title: {
                text: 'New Component'
              }
            },
            sideMenu: {
              left: {
              enabled: false
              }
            }
          }
        }
      });
    }

BroadcastListener

class MainActivity : AppCompatActivity() {

private val smsBroadcastReceiver by lazy { SMSBroadcastReceiver() }

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    val client = SmsRetriever.getClient(this)
    val retriever = client.startSmsRetriever()
    retriever.addOnSuccessListener {
        val listener = object : SMSBroadcastReceiver.Listener {
            override fun onReceived(pin: String) {
                // obtained pin
            }
            override fun onTimeOut() {
              //TimeOut
            }
        }
        smsBroadcastReceiver.injectListener(listener)
        registerReceiver(smsBroadcastReceiver,IntentFilter(SmsRetriever.SMS_RETRIEVED_ACTION))
    }
    retriever.addOnFailureListener {
      //Problem to start listener
    }
}

override fun onDestroy() {
    super.onDestroy()
    unregisterReceiver(smsBroadcastReceiver)
}

0 个答案:

没有答案