未解决的参考:getIntentSender / 尝试在 Jetpack Compose 中获取 phone 号码时

Unresolved reference: getIntentSender / While trying to obtain phone number in Jetpack Compose

我正在尝试在 Googles Phone Number Hint Docs 之后在 Jetpack compose 中获取 phone 个数字。但我遇到了一个问题,上面写着:getIntentSender()request: GetPhoneNumberHintIntentRequest.

中未解决

我在 addOnFailureListener

上也遇到了另一个错误
Type mismatch.
Required:
 OnFailureListener
Found:
 Int
@Composable
fun PhoneNumberConsent() {
    val context = LocalContext.current

    val request = GetPhoneNumberHintIntentRequest.builder().build()

    val phoneNumberHintIntentResultLauncher = rememberLauncherForActivityResult(
        contract = ActivityResultContracts.StartActivityForResult(),
    ) {
        try {
            val phoneNumber =
                Identity.getSignInClient(context)
                    .getPhoneNumberFromIntent(it.data)
        } catch (e: Exception) {
            Log.e(TAG, "Phone Number Hint failed")
        }

    }

    Identity.getSignInClient(context)
        .getPhoneNumberHintIntent(request)
        .addOnSuccessListener(
            try {
                phoneNumberHintIntentResultLauncher.launch(request.getIntentSender())
            } catch (e: Exception) {
                Log.e(TAG, "Launching the PendingIntent failed")
            } as OnSuccessListener<in PendingIntent>
        )
        .addOnFailureListener(
            Log.e(TAG, "Phone Number Hint failed")
        )
}

根据 this docs GetPhoneNumberHintIntentRequest class 中没有 getIntentSender() 方法。也许您正在阅读的教程中有错字,请尝试使用 result 而不是 request:

Identity.getSignInClient(context)
    .getPhoneNumberHintIntent(request)
    .addOnSuccessListener { result ->
        try {
            phoneNumberHintIntentResultLauncher.launch(result.intentSender.sendIntent)
        } catch (e: Exception) {
            Log.e(TAG, "Launching the PendingIntent failed")
        } as OnSuccessListener<in PendingIntent>
    }
    .addOnFailureListener(
        Log.e(TAG, "Phone Number Hint failed")
    )

addOnSuccessListener 接受一个监听器,它可以作为尾随闭包传递。

传递给此侦听器的结果是具有 intentSender 属性 的待定意图,它可用于创建 IntentSenderRequest.

这是一个工作示例:

val context = LocalContext.current

val request = GetPhoneNumberHintIntentRequest.builder().build()

val phoneNumberHintIntentResultLauncher = rememberLauncherForActivityResult(
    contract = ActivityResultContracts.StartIntentSenderForResult(),
) {
    try {
        val phoneNumber = Identity.getSignInClient(context)
                .getPhoneNumberFromIntent(it.data)
        println("phoneNumber $phoneNumber")
    } catch (e: Exception) {
        println("Phone Number Hint failed")
        e.printStackTrace()
    }

}

Button(onClick = {
    Identity.getSignInClient(context)
        .getPhoneNumberHintIntent(request)
        .addOnSuccessListener { pendingIntent ->
            try {
                phoneNumberHintIntentResultLauncher.launch(
                    IntentSenderRequest.Builder(
                        pendingIntent.intentSender
                    ).build()
                )
            } catch (e: Exception) {
                println("Launching the PendingIntent failed")
                e.printStackTrace()
            }
        }
        .addOnFailureListener {
            println("addOnFailureListener $it")
        }
}) {

}

如果您需要在视图出现后立即 运行 它,请使用 LaunchedEffect 而不是 Button.onClick。您当前的方法与 Compose 的基本规则之一相矛盾,即可组合函数必须不含 side-effects。在 thinking in compose

中阅读更多内容