startIntentSenderForResult 已弃用 Android

Posted

技术标签:

【中文标题】startIntentSenderForResult 已弃用 Android【英文标题】:startIntentSenderForResult deprecated Android 【发布时间】:2021-08-31 02:55:39 【问题描述】:

代码sn-p:

private fun requestHint() 
    val hintRequest = HintRequest.Builder()
        .setPhoneNumberIdentifierSupported(true)
        .build()
    val credentialsClient = Credentials.getClient(this)
    val intent = credentialsClient.getHintPickerIntent(hintRequest)
    try 
        startIntentSenderForResult(
            intent.intentSender,
            CREDENTIAL_PICKER_REQUEST,
            null, 0, 0, 0
        )
     catch (e: IntentSender.SendIntentException) 
        e.printStackTrace()
    

我试图从 documentation 解决这个问题,但我不明白

如果有人知道如何解决,请帮忙 谢谢

【问题讨论】:

【参考方案1】:

使用下面的代码 -

private fun showPhoneNumberSuggestion() 

    val hintRequest = HintRequest.Builder()
        .setPhoneNumberIdentifierSupported(true)
        .build()

    val options = CredentialsOptions.Builder()
        .forceEnableSaveDialog()
        .build()

    // Then pass the hint request object to
    // credentialsClient.getHintPickerIntent()
    // to get an intent to prompt the user to
    // choose a phone number.

    val credentialClient = Credentials.getClient(applicationContext, options)
    val intent = credentialClient.getHintPickerIntent(hintRequest)
    try 

        val intentSenderRequest = IntentSenderRequest.Builder(intent.intentSender).build()
        phonePickIntentResultLauncher.launch(intentSenderRequest)

     catch (e: IntentSender.SendIntentException) 
        e.printStackTrace()
    

对于 ActivityResult,使用 -

private val phonePickIntentResultLauncher =
    registerForActivityResult(ActivityResultContracts.StartIntentSenderForResult())  result ->
        if (result != null) 
            val intent = result.data
            val credential = intent?.getParcelableExtra<Credential>(Credential.EXTRA_KEY)
            binding.inputPhone.setText(credential?.id)
        
    

记住没有请求代码。

为每个特定任务使用不同的结果启动器。

【讨论】:

以上是关于startIntentSenderForResult 已弃用 Android的主要内容,如果未能解决你的问题,请参考以下文章