如何在Android中使用带有SMSRetriver API的GoogleAPIClient(不建议使用) [英] How to use GoogleAPIClient (deprecated) with SMSRetriver API in Android
问题描述
我正在尝试实施SMS Retriever API进行SMS验证.文档中提到的官方方式表示要与HintRequest
一起使用GoogleApiClient
从设备中检索手机号码
I am trying to implement SMS Retriever API for SMS verification. The official way mentioned in the documentation says to use GoogleApiClient
along with HintRequest
to retrieve the mobile number from the device
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
并由GoogleApi
界面,例如GoogleSignInClient
.我尝试使用GoogleSignInClient
,但getHintPickerIntent
不接受.即使不推荐使用旧的API还是安全的,还是可以通过SMSRetriver API使用旧的API?
But the GoogleAPIClient
is deprecated and replaced by GoogleApi
interface, such as GoogleSignInClient
. I tried to use GoogleSignInClient
but getHintPickerIntent
does not accept it. Is it safe to use the old API even after being deprecated or is there a way to use the latter with SMSRetriver API?
推荐答案
要删除不推荐使用的GoogleApiClient
,请使用以下内容替换您的意图:
To remove the deprecated GoogleApiClient
, replace your intent with the following:
// 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
.
Credentials
is found in this package: com.google.android.gms.auth.api.credentials.Credentials
.
完整的示例,当按下按钮时调用buttonClicked
:
Full working example which calls buttonClicked
when a button is pressed:
// 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()
}
}
}
这将生成如下所示的弹出窗口:
This will generate a popup like this:
这篇关于如何在Android中使用带有SMSRetriver API的GoogleAPIClient(不建议使用)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!