gpt4 book ai didi

android - 如何在 Android 中将 GoogleAPIClient(已弃用)与 SMSRetriver API 一起使用

转载 作者:行者123 更新时间:2023-12-04 23:36:24 47 4
gpt4 key购买 nike

我正在尝试实现 SMS Retriever API 以进行 SMS 验证。文档中提到的官方方式说使用 GoogleApiClient连同 HintRequest从设备中检索手机号码

HintRequest hintRequest = new HintRequest.Builder()
.setPhoneNumberIdentifierSupported(true)
.build();

PendingIntent intent = Auth.CredentialsApi.getHintPickerIntent(
googleApiClient, hintRequest);
try {
startIntentSenderForResult(intent.getIntentSender(),
RESOLVE_HINT, null, 0, 0, 0);
} catch (IntentSender.SendIntentException e) {
e.printStackTrace();
}

但是 GoogleAPIClient is deprecated and replaced by GoogleApi interface ,如 GoogleSignInClient .我尝试使用 GoogleSignInClient但是 getHintPickerIntent不接受。即使在被弃用之后使用旧 API 是否安全,或者有没有办法将后者与 SMSRetriver API 一起使用?

最佳答案

删除已弃用的 GoogleApiClient ,将您的 Intent 替换为以下内容:

// Kotlin
val intent = Credentials.getClient(this).getHintPickerIntent(hintRequest)
// Java
PendingIntent intent = Credentials.getClient(this).getHintPickerIntent(hintRequest);
Credentials在此包中找到: com.google.android.gms.auth.api.credentials.Credentials .

调用 buttonClicked 的完整工作示例按下按钮时:
// Kotlin

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.google.android.gms.auth.api.credentials.Credential
import com.google.android.gms.auth.api.credentials.Credentials
import com.google.android.gms.auth.api.credentials.CredentialsApi
import com.google.android.gms.auth.api.credentials.HintRequest

class MyActivity : AppCompatActivity() {

// ... onCreate Functions, etc

// Arbitrary number to identify the request for crednetials
private val iRequestCodePhoneNumber = 100

// Button click listener
fun buttonClicked(@Suppress("UNUSED_PARAMETER") view: View) {
val hintRequest = HintRequest.Builder()
.setPhoneNumberIdentifierSupported(true)
.build()

val intent = Credentials.getClient(this).getHintPickerIntent(hintRequest)

startIntentSenderForResult(
intent.intentSender,
iRequestCodePhoneNumber, null, 0, 0, 0
)
}

// Parse the result of the HintPicker (i.e., get the selected phone number)
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
// resultCode:
// Activity.RESULT_OK (-1) = number selected
// Activity.RESULT_CANCELED (0) = user touched outside the HintPicker (do nothing)
// CredentialsApi.ACTIVITY_RESULT_OTHER_ACCOUNT (1001) = "None of the above" (do nothing; treat as same use case as 'Cancelling')
// CredentialsApi.ACTIVITY_RESULT_NO_HINTS_AVAILABLE (1002) = no numbers found, probably no SIM card
if (requestCode == iRequestCodePhoneNumber && resultCode == Activity.RESULT_OK) {
val credential: Credential? = data?.getParcelableExtra(Credential.EXTRA_KEY)
val phoneNumber = credential?.id

// *** Do something with the phone number here ***

} else if (
requestCode == iRequestCodePhoneNumber &&
resultCode == CredentialsApi.ACTIVITY_RESULT_NO_HINTS_AVAILABLE
) {
// *** No phone numbers available ***
Toast.makeText(this, "No phone numbers found", Toast.LENGTH_LONG).show()
}
}
}

这将生成一个像这样的弹出窗口:

User selecting phone number available on their device

关于android - 如何在 Android 中将 GoogleAPIClient(已弃用)与 SMSRetriver API 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60690008/

47 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com