Skip to content Skip to sidebar Skip to footer

How To Use Googleapiclient (deprecated) With Smsretriver Api In Android

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

Solution 1:

To remove the deprecated GoogleApiClient, replace your intent with the following:

// Kotlinval intent = Credentials.getClient(this).getHintPickerIntent(hintRequest)
// JavaPendingIntentintent= Credentials.getClient(this).getHintPickerIntent(hintRequest);

Credentials is found in this package: com.google.android.gms.auth.api.credentials.Credentials.


Full working example which calls buttonClicked when a button is pressed:

// Kotlinimport 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

classMyActivity : AppCompatActivity() {

    // ... onCreate Functions, etc// Arbitrary number to identify the request for crednetialsprivateval iRequestCodePhoneNumber = 100// Button click listenerfunbuttonClicked(@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)overridefunonActivityResult(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 cardif (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 ***

        } elseif (
            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:

User selecting phone number available on their device

Solution 2:

Use this function requestHint() when you want to request for phone number hint.

privatevoidrequestHint() {
    HintRequesthintRequest=newHintRequest.Builder()
            .setPhoneNumberIdentifierSupported(true)
            .build();
    PendingIntentintent= Credentials.getClient(this).getHintPickerIntent(hintRequest);
    IntentSenderRequest.BuilderintentSenderRequest=newIntentSenderRequest.Builder(intent.getIntentSender());
    hintLauncher.launch(intentSenderRequest.build());
}

Here, we use this to handle the result (previously, we used onActivityResult() for this).

ActivityResultLauncher<IntentSenderRequest> hintLauncher = registerForActivityResult(newActivityResultContracts.StartIntentSenderForResult(),
    result -> {
        if(result!=null && result.getData()!=null){
                Intentdata= result.getData();
                Credentialcredential= data.getParcelableExtra(Credential.EXTRA_KEY);
                StringphoneNum= credential.getId();
                if (phoneNum.contains("+91"))
                    phoneNum = phoneNum.replace("+91", "");
                mPhoneEditText.setText(phoneNum);
        }
});

Post a Comment for "How To Use Googleapiclient (deprecated) With Smsretriver Api In Android"