1

i have setup google play services. when i sign in for the first time in device it works and signs in. but when i restart the app it does not sign in and returns null.

can any one tell me proper steps to configure google play game services please ?

private 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"+task.getResult());
                            Toast.makeText(SetupActivity.this, ""+task.getResult().getEmail(), Toast.LENGTH_SHORT).show();
                            //onConnected(task.getResult());
                        } else {
                            Log.d(TAG, "signInSilently(): failure", task.getException());
                            //onDisconnected();
                            startSignInIntent();
                        }
                    }
                });
    }

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

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

        if (!isSignedIn()) {
            Log.w(TAG, "signOut() called, but was not signed in!");
            Toast.makeText(this, "not sign in", Toast.LENGTH_SHORT).show();
            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"));

                        if (successful)
                            Toast.makeText(SetupActivity.this, "success logout", Toast.LENGTH_SHORT).show();
                        else
                            Toast.makeText(SetupActivity.this, "no", Toast.LENGTH_SHORT).show();
                       // onDisconnected();
                    }
                });
    }

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

            try {
                GoogleSignInAccount account = task.getResult(ApiException.class);
                Toast.makeText(this, ""+account.getEmail(), Toast.LENGTH_SHORT).show();
                //           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();
            }
        }
    }

oncreate

mGoogleSignInClient = GoogleSignIn.getClient(this, new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN).build());

    //signInSilently();

    startSignInIntent();
Nick Fortescue
  • 13,530
  • 1
  • 31
  • 37
Yash Mehta
  • 43
  • 1
  • 6
  • Do you want to force sign-in every app start? Try checking this related [SO post](https://stackoverflow.com/a/34706726/5995040) if it will solve your issue. – Mr.Rebot Jul 19 '18 at 10:44
  • NO i dont want to force login every time. Even if i do signin sliently, it show null in account. and now achievements are displayed in play play games for the game. I want to add google play services in my game but it works only 1 time in new device which i try then it show null in getEmail or display name. Login is not successfull or which error is not displayed. I dont know to setup google play services. I did exactly the way it was guided in developer guides of google. Then too i m unable to setup google play games services for my game. – Yash Mehta Jul 20 '18 at 18:15

0 Answers0