I am trying get phone number using phone selector API, but get empty response, here my code.
GoogleApiClient googleApiClient = new GoogleApiClient.Builder(this)
.addApi(Auth.CREDENTIALS_API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
googleApiClient.connect();
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();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESOLVE_HINT) {
if (resultCode == RESULT_OK) {
Credential credential = data.getParcelableExtra(Credential.EXTRA_KEY);
// credential.getId(); <-- will need to process phone number string
}
}
}
Above resultCode always returns 0.
Here is what i did to get Number:
Note: I'm getting numbers inside Fragment.
1) get GoogleApiClient
private void getCreadenticalApiClient() {
mCredentialsApiClient = new GoogleApiClient.Builder(getBaseContext())
.addConnectionCallbacks(this)
.enableAutoManage(getBaseContext(), this)
.addApi(Auth.CREDENTIALS_API)
.build();
}
2) showHint method will show to popup with number:
private void showHint() {
HintRequest hintRequest = new HintRequest.Builder()
.setHintPickerConfig(new CredentialPickerConfig.Builder()
.setShowCancelButton(true)
.build())
.setPhoneNumberIdentifierSupported(true)
.build();
PendingIntent intent =
Auth.CredentialsApi.getHintPickerIntent(mCredentialsApiClient, hintRequest);
try {
startIntentSenderForResult(intent.getIntentSender(), RC_HINT, null, 0, 0, 0,new Bundle());
} catch (IntentSender.SendIntentException e) {
Log.e("Login", "Could not start hint picker Intent", e);
}
}
3) And this is where you assign number to View
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_HINT) {
if (resultCode == RESULT_OK) {
Credential cred = 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'