Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Android中使用GoogleAppClient(已弃用)和SMSRetriever API_Android_Google Api_Google Api Client_Googlesigninapi_Sms Retriever Api - Fatal编程技术网

如何在Android中使用GoogleAppClient(已弃用)和SMSRetriever API

如何在Android中使用GoogleAppClient(已弃用)和SMSRetriever API,android,google-api,google-api-client,googlesigninapi,sms-retriever-api,Android,Google Api,Google Api Client,Googlesigninapi,Sms Retriever Api,我试图实现短信检索器API的短信验证。文档中提到的官方方式是使用GoogleApiClient和HintRequest从设备中检索手机号码 HintRequest hintRequest = new HintRequest.Builder() .setPhoneNumberIdentifierSupported(true) .build(); PendingIntent intent = Auth.CredentialsApi.getHintPi

我试图实现短信检索器API的短信验证。文档中提到的官方方式是使用
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();
}

但是,这是一个很好的例子,比如
GoogleSignInClient
。我试图使用
GoogleSignInClient
,但
getHintPickerIntent
不接受它。即使在被弃用后使用旧API是否安全,或者有没有办法将后者与SMSRetriever API一起使用?

要删除弃用的
GoogleAppClient
,请用以下内容替换您的意图:

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


按下按钮时调用按钮单击的完整工作示例:

// 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()
        }
    }
}
这将生成如下弹出窗口:


我同情任何在这个答案上结结巴巴的人,因为从不推荐的
GoogleAppClient
s迁移的文档非常差(没有为旧/新类或技术提供映射)。希望这能帮助到别人。谢谢你,克里斯托弗!它工作得很好。我完全同意您关于文档的看法。感谢您的分享,希望我能做更多的投票。有人能告诉我们应该在函数
buttonClicked(View View)
视图位置传递什么吗?如果我在Java中使用它,您不应该直接调用
buttonClicked(View View)
。在XML布局文件中,选择一个按钮(它是
视图
的子类),并将其onclick事件侦听器设置为“buttonClicked”。如果你做得对,Android Studio应该自动推荐可用的方法。
// 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()
        }
    }
}