Help with google play sign in

Anything about development not directly related to libgdx, e.g. OpenGL, Android APIs etc.

Help with google play sign in

Postby DRing » Sun Feb 18, 2018 5:12 pm

Im looking for some help on implementing Google Play sign in. Ive got the interface part figured out, but the last time I did this, google had a gamehelper utility and now it doesnt and having trouble figuring out how to implement the sign in, so after they sign in the first time it auto signs in when the app starts. Also cant seem to think of how ot send the info back to update the display with their new info...Heres what I got:

Main Game uses an interface and calls the sign in function on button click:
Code: Select all
TextButton profile = new TextButton("Login",skin, "default");
        profile.addListener(new ClickListener(){
            @Override
            public void clicked(InputEvent event, float x, float y) {
                game.playServices.signIn();
            }
        });


Then taking the code from one of the examples from the google source this is my android launcher:

Code: Select all
//Client used to sign in with Google APIs
   private GoogleSignInClient mGoogleSignInClient;

   //request codes we use when invoking an external activity
   private static final int RC_SIGN_IN = 9001;

   //Client variables
   private PlayersClient mPlayersClient;

@Override
   protected void onResume() {
      super.onResume();
      Log.d(TAG, "onResume()");

      // Since the state of the signed in user can change when the activity is not active
      // it is recommended to try and sign in silently from when the app resumes.
      signInSilently();
   }



   @Override
   public void signIn() {
      startSignInIntent();
   }

   @Override
   public void signOut() {
      Log.d(TAG, "signOut()");

      if (!isSignedIn()) {
         Log.w(TAG, "signOut() called, but was not signed in!");
         return;
      }

      mGoogleSignInClient.signOut().addOnCompleteListener(this,
            new OnCompleteListener<Void>() {
               @Override
               public void onComplete(@NonNull Task<Void> task) {
                  boolean successful = task.isSuccessful();
                  Log.d(TAG, "signOut(): " + (successful ? "success" : "failed"));

                  onDisconnected();
               }
            });
   }

@Override
   public boolean isSignedIn() {
      return GoogleSignIn.getLastSignedInAccount(this) != null;
   }

private void signInSilently(){
      Log.d(TAG, "signInSilently()");

      mGoogleSignInClient.silentSignIn().addOnCompleteListener(this,
            new OnCompleteListener<GoogleSignInAccount>() {
               @Override
               public void onComplete(@NonNull Task<GoogleSignInAccount> task) {
                  if (task.isSuccessful()) {
                     Log.d(TAG, "signInSilently(): success");

                     onConnected(task.getResult());
                  } else {
                     Log.d(TAG, "signInSilently(): failure", task.getException());
                     onDisconnected();
                  }
               }
            });
   }

   private void startSignInIntent() {
      startActivityForResult(mGoogleSignInClient.getSignInIntent(), RC_SIGN_IN);
   }

   @Override
   protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      super.onActivityResult(requestCode, resultCode, data);
      if (requestCode == RC_SIGN_IN) {
         Task<GoogleSignInAccount> task =
               GoogleSignIn.getSignedInAccountFromIntent(data);

         try {
            GoogleSignInAccount account = task.getResult(ApiException.class);
            onConnected(account);
         } catch (ApiException apiException) {
            String message = apiException.getMessage();
            if (message == null || message.isEmpty()) {
               message = getString(R.string.signin_other_error);
            }

            onDisconnected();

            new AlertDialog.Builder(this)
                  .setMessage(message)
                  .setNeutralButton(android.R.string.ok, null)
                  .show();
         }
      }
   }

   private void onDisconnected() {
      Log.d(TAG, "onDisconnected()");

      mPlayersClient = null;
      // Show sign-in button on main menu

   }

   private void onConnected(GoogleSignInAccount googleSignInAccount) {
      Log.d(TAG, "onConnected(): connected to Google APIs");

      GamesClient gamesClient = Games.getGamesClient(AndroidLauncher.this, googleSignInAccount);
      gamesClient.setGravityForPopups(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
      gamesClient.setViewForPopups(((AndroidGraphics) Gdx.graphics).getView());

      mPlayersClient = Games.getPlayersClient(this, googleSignInAccount);

      // Show sign-out button on main menu

      // Set the greeting appropriately on main menu
      mPlayersClient.getCurrentPlayer()
            .addOnCompleteListener(new OnCompleteListener<Player>() {
               @Override
               public void onComplete(@NonNull Task<Player> task) {
                  String displayName;
                  if (task.isSuccessful()) {
                     displayName = task.getResult().getDisplayName();
                  } else {
                     Exception e = task.getException();
                     handleException(e, getString(R.string.players_exception));
                     displayName = "???";
                  }
                  //mMainMenuFragment.setGreeting("Hello, " + displayName);
               }
            });


      // if we have accomplishments to push, push them
      //if (!mOutbox.isEmpty()) {
      //   pushAccomplishments();
      //   Toast.makeText(this, getString(R.string.your_progress_will_be_uploaded),
      //         Toast.LENGTH_LONG).show();
      //}

      //loadAndPrintEvents();

   }

   private void handleException(Exception e, String details) {
      int status = 0;

      if (e instanceof ApiException) {
         ApiException apiException = (ApiException) e;
         status = apiException.getStatusCode();
      }

      String message = getString(R.string.status_exception_error, details, status, e);

      new AlertDialog.Builder(AndroidLauncher.this)
            .setMessage(message)
            .setNeutralButton(android.R.string.ok, null)
            .show();
   }


I think it logs in as I get the log output of success
D/RoboSmash: signInSilently(): success
D/RoboSmash: onConnected(): connected to Google APIs

But it just flashes like it is going to another activity real quick and then comes back. Doesnt show the login or the little alert at the top like other games
DRing
 
Posts: 104
Joined: Thu Oct 20, 2011 7:42 pm

Return to General Development

Who is online

Users browsing this forum: No registered users and 1 guest