要删除 deprecated GoogleApiClient
,请将您的意图替换为以下内容:
// 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()
}
}
}
这将生成一个像这样的弹出窗口: