我正在做一个你用谷歌登录的 Activity ,我拥有的一切都很好,但我需要检查用户是否已经登录,然后将我定向到“MainActivity”
相关代码:
private void initLogInWithGoogle(){
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build();
mGoogleApi = new GoogleApiClient.Builder(this).enableAutoManage(this, new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
}).addApi(Auth.GOOGLE_SIGN_IN_API, gso).build();
}
private void sigInGoogle(){
Intent intent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApi);
startActivityForResult(intent, RC_SIGN_IN);
}
private void handleResult(GoogleSignInResult result){
if(result.isSuccess()){
Intent intent1 = new Intent(RegisterActivity.this, UserActivity.class);
intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent1);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
if(requestCode == RC_SIGN_IN){
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
handleResult(result);
}
}
最佳答案
您可以使用 user.getProviders().get(0) 检查身份验证提供程序;这将返回登录提供程序。如果 user!=null,请在 AuthStateListener 中使用它。
关于android - 如何检查用户是否使用谷歌登录,以及是否直接定向到 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43578853/