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(); }