Requests to retrieve user credentials can fail when a user has not yet saved credentials or when a user has not yet signed up to your app. In these situations, use the Credentials API to retrieve sign-in hints, such as the user's name and email address. Use these hints to pre-fill your app's sign-in and sign-up forms, speeding up your app's on-boarding process.
On Android 6.0 (Marshmallow) and newer, your app does not need to request any device or runtime permissions to retrieve sign-in hints with the Credentials API.
Before you begin
Configure an Android Studio project .
Retrieve sign-in hints
To retrieve the sign-in hints, first configure the hint selector dialog by
creating a HintRequest
object. Then, pass the HintRequest
object to CredentialsClient.getHintPickerIntent()
to get an intent to prompt the user to choose an email address. Finally, start the
intent with startIntentSenderForResult()
.
HintRequest hintRequest = new HintRequest.Builder()
.setHintPickerConfig(new CredentialPickerConfig.Builder()
.setShowCancelButton(true)
.build())
.setEmailAddressIdentifierSupported(true)
.setAccountTypes(IdentityProviders.GOOGLE)
.build();
PendingIntent intent = mCredentialsClient.getHintPickerIntent(hintRequest);
try {
startIntentSenderForResult(intent.getIntentSender(), RC_HINT, null, 0, 0, 0);
} catch (IntentSender.SendIntentException e) {
Log.e(TAG, "Could not start hint picker Intent", e);
}
The user is prompted to choose an email address to use.
Then, in the activity's onActivityResult()
method, retrieve the hints from the Credential.EXTRA_KEY
parcel, check whether the user is in your user database,
and start the appropriate activity with the credentials hint.
@Override public void onActivityResult ( int requestCode , int resultCode , Intent data ) { super . onActivityResult ( requestCode , resultCode , data ); if ( requestCode == RC_HINT ) { if ( resultCode == RESULT_OK ) { Credential credential = data . getParcelableExtra ( Credential . EXTRA_KEY ); Intent intent ; // Check for the user ID in your user database . if ( userDatabaseContains ( credential . getId ())) { intent = new Intent ( this , SignInActivity . class ); } else { intent = new Intent ( this , SignUpNewUserActivity . class ); } intent . putExtra ( "com.mycompany.myapp.SIGNIN_HINTS" , credential ); startActivity ( intent ); } else { Log . e ( TAG , "Hint Read: NOT OK" ); Toast . makeText ( this , "Hint Read Failed" , Toast . LENGTH_SHORT ). show (); } } ... }
Pre-fill the sign-in form
If the user is in your user database and you started your app's sign-in
activity, you can (optionally) check if the Credential
object contains an ID
token. If so, you can sign in the user with the ID token
,
without requiring the user to type a password.
If the Credential
object doesn't contain an ID token (or you don't want to use
the ID token), pre-fill the sign-in fields with the hints that you added to the
intent.
public
class
SignInActivity
extends
Activity
{
@
Override
protected
void
onCreate
(
Bundle
savedInstanceState
)
{
super
.
onCreate
(
savedInstanceState
);
Intent
intent
=
getIntent
();
Credential
credential
=
intent
.
getParcelableExtra
(
"com.mycompany.myapp.SIGNIN_HINTS"
);
//
Pre
-
fill
ID
field
mUsernameView
.
setText
(
credential
.
getId
());
...
}
...
}
Pre-fill the sign-up form
If the user isn't in your user database and you started your app's sign-up activity, pre-fill the sign-up fields with the sign-in hints that you added to the intent.
public
class
SignUpNewUserActivity
extends
Activity
{
@
Override
protected
void
onCreate
(
Bundle
savedInstanceState
)
{
super
.
onCreate
(
savedInstanceState
);
Intent
intent
=
getIntent
();
Credential
credential
=
intent
.
getParcelableExtra
(
"com.mycompany.myapp.SIGNIN_HINTS"
);
//
Pre
-
fill
sign
-
up
fields
mUsernameView
.
setText
(
credential
.
getId
());
mDisplaynameView
.
setText
(
credential
.
getName
());
//
Might
be
null
.
...
}
...
}
Optionally, you can also check if the Credential
object contains an ID token
that has a
verified email address. If so, you can skip your app's email verification step,
since the email address has already been verified by Google.