Skip to content Skip to sidebar Skip to footer

Phone Selector Api In Android

I am trying get phone number using phone selector API, but get empty response, here my code. GoogleApiClient googleApiClient = new GoogleApiClient.Builder(this) .addApi

Solution 1:

Here is what i did to get Number:

Note: I'm getting numbers inside Fragment.

1) get GoogleApiClient

privatevoidgetCreadenticalApiClient() {
        mCredentialsApiClient = new GoogleApiClient.Builder(getBaseContext())
                .addConnectionCallbacks(this)
                .enableAutoManage(getBaseContext(), this)
                .addApi(Auth.CREDENTIALS_API)
                .build();
    }

2) showHint method will show to popup with number:

privatevoidshowHint() {
        HintRequesthintRequest=newHintRequest.Builder()
                .setHintPickerConfig(newCredentialPickerConfig.Builder()
                        .setShowCancelButton(true)
                        .build())
                .setPhoneNumberIdentifierSupported(true)
                .build();

        PendingIntentintent=
                Auth.CredentialsApi.getHintPickerIntent(mCredentialsApiClient, hintRequest);
        try {
            startIntentSenderForResult(intent.getIntentSender(), RC_HINT, null, 0, 0, 0,newBundle());
        } catch (IntentSender.SendIntentException e) {
            Log.e("Login", "Could not start hint picker Intent", e);
        }
    }

3) And this is where you assign number to View

@OverridepublicvoidonActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == RC_HINT) {
            if (resultCode == RESULT_OK) {
                Credentialcred= data.getParcelableExtra(Credential.EXTRA_KEY);
                etMobile.setText(cred.getId().substring(3));
            }
        }
    }

At last do not forget to implement these two interfaces inside fragment:

GoogleApiClient.ConnectionCallbacks
GoogleApiClient.OnConnectionFailedListener

dependencies:

    implementation 'com.google.android.gms:play-services-base:11.8.0'
    implementation 'com.google.android.gms:play-services-identity:11.8.0'
    implementation 'com.google.android.gms:play-services-auth:11.8.0'
    implementation 'com.google.android.gms:play-services-auth-api-phone:11.8.0'

Solution 2:

I got success to get the mobile number by using mobile selector api with hint request for you need to add some following dependency in app level gradle

implementation 'com.google.android.gms:play-services-auth:17.0.0'
implementation 'com.google.android.gms:play-services-identity:17.0.0'
implementation 'com.google.android.gms:play-services-auth-api-phone:17.1.0'

After that you have to implement your activity with googleAiClient like following

publicclassMainActivityextendsAppCompatActivityimplementsGoogleApiClient.ConnectionCallbacks,
 GoogleApiClient.OnConnectionFailedListener{{

after that put request and manage it in onActivityForresult like folowing

GoogleApiClientgoogleApiClient=newGoogleApiClient.Builder(this)
                .addApi(Auth.CREDENTIALS_API)
                .addConnectionCallbacks((GoogleApiClient.ConnectionCallbacks) MainActivity.this)
                .addOnConnectionFailedListener((GoogleApiClient.OnConnectionFailedListener) MainActivity.this)
                .build();
        googleApiClient.connect();
        HintRequesthintRequest=newHintRequest.Builder()
                .setPhoneNumberIdentifierSupported(true)
                .build();
        PendingIntentintent= Auth.CredentialsApi.getHintPickerIntent(googleApiClient, hintRequest);
        try {
            startIntentSenderForResult(intent.getIntentSender(),
                    RESOLVE_HINT, null, 0, 0, 0);
        } catch (IntentSender.SendIntentException e) {
            e.printStackTrace();
        }

And handle it like folowwing

@OverridepublicvoidonActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == RESOLVE_HINT) {
            if (resultCode == RESULT_OK) {
                com.google.android.gms.auth.api.credentials.Credentialcredential= data.getParcelableExtra(Credential.EXTRA_KEY);
                if (credential != null) {

                    mobNumber = credential.getId();
                    StringnewString= mobNumber.replace("+91", "");
                    Toast.makeText(this, "" + newString, Toast.LENGTH_SHORT).show();

                } else {
                    Toast.makeText(this, "err", Toast.LENGTH_SHORT).show();
                }
            }
        }
    }

for more visit phone Slector Api

Solution 3:

The GoogleApiClient is now deprecated, please follow the following steps, to implement the same.

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

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

// Java
PendingIntent intent = 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()
        }
    }
}

Follow this question for more clarity: How to use GoogleAPIClient (deprecated) with SMSRetriver API in Android

Credit to: Christopher Bull

Post a Comment for "Phone Selector Api In Android"