Task
private GoogleSignInClient signInClient; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // other code here GoogleSignInOptions signInOption = new GoogleSignInOptions.Builder( GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN) // If you are using Snapshots add the Drive scope. .requestScopes(Drive.SCOPE_APPFOLDER) // If you need a server side auth code, request it here. .requestServerAuthCode(webClientId) .build(); signInClient = GoogleSignIn.getClient(context, signInOption); }
private void signInSilently() { GoogleSignInOptions signInOption = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN) .build(); GoogleSignInClient signInClient = GoogleSignIn.getClient(this, signInOption); signInClient.silentSignIn().addOnCompleteListener(this, new OnCompleteListener<GoogleSignInAccount>() { @Override public void onComplete(@NonNull Task<GoogleSignInAccount> task) { // Handle UI updates based on being signed in or not. enableUIButtons(task.isSuccessful()); // It is OK to cache the account for later use. mSignInAccount = task.getResult(); } }); }
@Override protected void onResume() { super.onResume(); signInSilently(); }
Intent intent = signInClient.getSignInIntent(); startActivityForResult(intent, RC_SIGN_IN);
@Override protected void onActivityResult(int requestCode, int resultCode, Intent intent) { super.onActivityResult(requestCode, resultCode, intent); if (requestCode == RC_SIGN_IN) { // The Task returned from this call is always completed, no need to attach // a listener. Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(intent); try { GoogleSignInAccount account = task.getResult(ApiException.class); // Signed in successfully, show authenticated UI. enableUIButtons(true); } catch (ApiException apiException) { // The ApiException status code indicates the // detailed failure reason. // Please refer to the GoogleSignInStatusCodes class reference // for more information. Log.w(TAG, "signInResult:failed code= " + apiException.getStatusCode()); new AlertDialog.Builder(MainActivity.this) .setMessage("Signin Failed") .setNeutralButton(android.R.string.ok, null) .show(); } } }
if (GoogleSignIn.getLastSignedInAccount(/*context*/ this) != null) { // There is a user signed in, handle updating the UI. enableUIButtons(true); } else { // Not signed in; update the UI. enableUIButtons(false); }
signInClient.signOut().addOnCompleteListener(MainActivity.this, new OnCompleteListener<Void>() { @Override public void onComplete(@NonNull Task<Void> task) { enableUIButtons(false); } }); );
PendingResult<Stats.LoadPlayerStatsResult> result = Games.Stats.loadPlayerStats( mGoogleApiClient, false /* forceReload */); result.setResultCallback(new ResultCallback<Stats.LoadPlayerStatsResult>() { public void onResult(Stats.LoadPlayerStatsResult result) { Status status = result.getStatus(); if (status.isSuccess()) { PlayerStats stats = result.getPlayerStats(); if (stats != null) { Log.d(TAG, "Player stats loaded"); if (stats.getDaysSinceLastPlayed() > 7) { Log.d(TAG, "It's been longer than a week"); } if (stats.getNumberOfSessions() > 1000) { Log.d(TAG, "Veteran player"); } if (stats.getChurnProbability() == 1) { Log.d(TAG, "Player is at high risk of churn"); } } } else { Log.d(TAG, "Failed to fetch Stats Data status: " + status.getStatusMessage()); } } });
GoogleSignInAccount mSignInAccount = null; Games.getPlayerStatsClient(this, mSignInAccount).loadPlayerStats(true) .addOnCompleteListener( new OnCompleteListener<AnnotatedData<PlayerStats>>() { @Override public void onComplete(Task<AnnotatedData<PlayerStats>> task) { try { AnnotatedData<PlayerStats> statsData = task.getResult(ApiException.class); if (statsData.isStale()) { Log.d(TAG,"using cached data"); } PlayerStats stats = statsData.get(); if (stats != null) { Log.d(TAG, "Player stats loaded"); if (stats.getDaysSinceLastPlayed() > 7) { Log.d(TAG, "It's been longer than a week"); } if (stats.getNumberOfSessions() > 1000) { Log.d(TAG, "Veteran player"); } if (stats.getChurnProbability() == 1) { Log.d(TAG, "Player is at high risk of churn"); } } } catch (ApiException apiException) { int status = apiException.getStatusCode(); Log.d(TAG, "Failed to fetch Stats Data status: " + status + ": " + task.getException()); } } });
Games.getGamesClient(MainActivity.this, mSignInAccount) .getActivationHint().addOnCompleteListener( new OnCompleteListener<Bundle>() { @Override public void onComplete(@NonNull Task<Bundle> task) { try { Bundle hint = task.getResult(ApiException.class); if (hint != null) { Invitation inv = hint.getParcelable(Multiplayer.EXTRA_INVITATION); if (inv != null && inv.getInvitationId() != null) { // retrieve and cache the invitation ID acceptInviteToRoom(inv.getInvitationId()); return; } } } catch (ApiException apiException) { Log.w(TAG, "getActivationHint failed: " + apiException.getMessage()); } } });
Task.isSuccessful()
Task.getException()
ApiException
if (task.getException() instanceof ApiException) { ApiException apiException = (ApiException) task.getException(); status = apiException.getStatusCode(); }
if (task.getException() instanceof MatchApiException) { MatchApiException matchApiException = (MatchApiException) task.getException(); status = matchApiException.getStatusCode(); match = matchApiException.getMatch(); } else if (task.getException() instanceof ApiException) { ApiException apiException = (ApiException) task.getException(); status = apiException.getStatusCode(); }
// Construct a request for phone numbers and show the picker private void requestHint() { HintRequest hintRequest = new HintRequest.Builder() .setPhoneNumberIdentifierSupported(true) .build(); PendingIntent intent = Auth.CredentialsApi.getHintPickerIntent( apiClient, hintRequest); startIntentSenderForResult(intent.getIntentSender(), RESOLVE_HINT, null, 0, 0, 0); }
// Obtain the phone number from the result @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(); <-- E.164 format phone number on 10.2.+ devices } } }
SmsRetrieverClient
SmsRetrieverClient client = SmsRetriever.getClient(this /* context */); Task<Void> task = client.startSmsRetriever(); task.addOnSuccessListener(new OnSuccessListener<Void>() { @Override public void onSuccess(Void aVoid) { // successfully started an SMS Retriever for one SMS message } }); task.addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { }); );
<#> Use 123456 as your verification code in Example App!
FA+9qCX9VSu
public class MySMSBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if (SmsRetriever.SMS_RETRIEVED_ACTION.equals(intent.getAction())) { Bundle extras = intent.getExtras(); Status status = (Status) extras.get(SmsRetriever.EXTRA_STATUS); switch(status.getStatusCode()) { case CommonStatusCodes.SUCCESS: String message = (String) extras.get(SmsRetriever.EXTRA_SMS_MESSAGE); break; case CommonStatusCodes.TIMEOUT: break; } } } }