我是一名新的 Android 开发人员,也是一名学生。我很难判断用户是否已经登录我的应用程序。我将展示我的一些代码。我非常感谢您的帮助和回答。
登录.java
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
OptionalPendingResult<GoogleSignInResult> opr = Auth.GoogleSignInApi.silentSignIn(mGoogleApiClient);
if(opr.isDone()) {
Log.d(TAG,"Got cached sign-in");
GoogleSignInResult result = opr.get();
handleSignInResult(result);
} else {
SignInButton mGoogleSignInButton = (SignInButton) findViewById(R.id.sign_in_button);
mStatusView = (TextView) findViewById(R.id.status_text);
mGoogleSignInButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.sign_in_button:
signInWithGoogle();
}
}
});
}
}
private void signInWithGoogle(){
if(mGoogleApiClient != null) {
mGoogleApiClient.disconnect();
}
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN).requestEmail().build();
mGoogleApiClient = new GoogleApiClient.Builder(this).addApi(Auth.GOOGLE_SIGN_IN_API,gso).build();
final Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, RC_SIGN_IN);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode,resultCode,data);
if(requestCode == RC_SIGN_IN) {
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
handleSignInResult(result);
}
}
private void handleSignInResult(GoogleSignInResult result) {
Log.d(TAG, "handleSignInResult:" + result.isSuccess());
if(result.isSuccess()) {
GoogleSignInAccount account = result.getSignInAccount();
mStatusView.setText(getString(R.string.signed_in_fmt, account.getDisplayName()));
//TODO: Start another explicit intent here. but this time, start MainActivity
Intent intent = new Intent(Login.this , MainActivity.class);
startActivity(intent);
finish();
} else {
}
}
我哪里出错了?
最佳答案
GoogleApiClient mGoogleApiClient 现已弃用! 所以根据doc
// Check for existing Google Sign In account, if the user is already signed in
// the GoogleSignInAccount will be non-null.
GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this);
updateUI(account);
例如:
if (GoogleSignIn.getLastSignedInAccount(context) == null) {
Log.d(TAG_SIGN_FLOW, "User is not signed in, so make login in")
val signInOptions: GoogleSignInOptions = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.requestScopes(Scope(DriveScopes.DRIVE_FILE))
.build()
startActivityForResult(GoogleSignIn.getClient(this.requireActivity(), signInOptions).signInIntent, REQUEST_CODE_SIGN_IN)
} else {
Log.d(TAG_SIGN_FLOW, "User has already been login")
}
关于java - 如何检查用户是否已经登录谷歌身份验证。在安卓中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40499018/