6

Tôi fount rằng để đăng nhập người sử dụng tôi phải sử dụng mã này:Kiểm tra xem người dùng đã đăng nhập bằng Auth.GoogleSignInApi chưa?

Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient); 
startActivityForResult(signInIntent, RC_SIGN_IN); 

để signout

new ResultCallback<Status>() { 
       @Override 
       public void onResult(Status status) { 
        disconnect(); 
       } 
      }); 

Nhưng khi người dùng relaunch ứng dụng và ông đã được đăng nhập (và không đăng xuất trước) có thể phát hiện trạng thái 'hiện đã đăng nhập' này không?

Rõ ràng, có thể lưu 'đăng nhập' trong cài đặt (tùy chọn chia sẻ) của ứng dụng nhưng là nơi phát hiện bằng cách sử dụng google api?

Trả lời

10

Here Tôi tìm thấy giải pháp:

OptionalPendingResult<GoogleSignInResult> opr = Auth.GoogleSignInApi.silentSignIn(mGoogleApiClient); 
     if (opr.isDone()) { 
      // If the user's cached credentials are valid, the OptionalPendingResult will be "done" 
      // and the GoogleSignInResult will be available instantly. 
      Log.d(TAG, "Got cached sign-in"); 
      GoogleSignInResult result = opr.get(); 
      handleSignInResult(result); 
     } else { 
      // If the user has not previously signed in on this device or the sign-in has expired, 
      // this asynchronous branch will attempt to sign in the user silently. Cross-device 
      // single sign-on will occur in this branch. 
      showProgressDialog(); 
      opr.setResultCallback(new ResultCallback<GoogleSignInResult>() { 
       @Override 
       public void onResult(GoogleSignInResult googleSignInResult) { 
        hideProgressDialog(); 
        handleSignInResult(googleSignInResult); 
       } 
      }); 
     } 
0

Ở đây tôi tìm thấy những giải pháp đơn giản cho điều này

GoogleSignInAccount lastSignedInAccount= GoogleSignIn.getLastSignedInAccount(context); 
if(lastSignedInAccount==null){ 
// user has already logged in, you can check user's email, name etc from lastSignedInAccount 
String email = lastSignedInAccount.getEmail(); 
}else{ 
// user is not logged in with any account 
} 
+0

Đây thực sự là một phiên bản mới hơn. Phiên bản SDK của dịch vụ Play phải là 11.6+. – Jenix

Các vấn đề liên quan