Silent Google Sign In in android background service

后端 未结 2 1974
不思量自难忘°
不思量自难忘° 2020-12-31 15:49

I am running a background service in my android app. I use the IdToken that I get from the sign in activity to authenticate at the backend server. The service is running in

2条回答
  •  长情又很酷
    2020-12-31 16:27

    Try moving the

    opr.setResultCallback(new ResultCallback() {
                @Override
                public void onResult(GoogleSignInResult googleSignInResult) {
                    Log.d(TAG, "got result");
                    handleSignInResult(googleSignInResult);
                }
            });
    

    to before its if statement. I know the google docs say to have it the way you do and I did the same until I realized that the ResultCallback will never get called if it only gets initialized in the else block, after opr has already run.

    Your code should look like this after following my suggestion:

    private void signIn() {
        new Thread(new Runnable() { public void run() {
        GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestEmail()
                .requestIdToken("")
                .build();
        GoogleApiClient mGoogleApiClient = new GoogleApiClient.Builder(context)
                .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
                .build();
    
        OptionalPendingResult opr = Auth.GoogleSignInApi.silentSignIn
                (mGoogleApiClient);
        opr.setResultCallback(new ResultCallback() {
                @Override
                public void onResult(GoogleSignInResult googleSignInResult) {
                    Log.d(TAG, "got result");
                    handleSignInResult(googleSignInResult);
                }
            });}}).start();
    }
    

提交回复
热议问题