android - 如果用户已经登录,在我的 android 应用程序中使用 google 登录的问题
问题描述
如果用户已经登录,我在我的应用程序中使用谷歌登录时遇到问题。当我第一次登录时它工作得很好,因为它要求我输入密码和其他信息但是如果用户已经登录,我通常会瘦应该打开像这样的选择帐户菜单:
[截图][https://androidclarified.com/wp-content/uploads/2018/11/Screenshot_2018-11-11-20-53-15-306_com.google.android.gms_-768x1365.png]
但是在我的情况下会发生以下情况:[problem-gif][https://gph.is/g/ZYBxy14]
public class LoginActivity extends AppCompatActivity implements
View.OnClickListener{
private static final String TAG = "LoginActivity1";
private static final int RC_SIGN_IN = 9001;
private GoogleSignInClient mGoogleSignInClient;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
SignInButton signInButton = findViewById(R.id.sign_in_button);
signInButton.setOnClickListener(this);
}
private void signIn() {
Toast.makeText(this, "button works", Toast.LENGTH_SHORT).show();
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
startActivityForResult(signInIntent, RC_SIGN_IN);
}
private void signOut() {
mGoogleSignInClient.signOut()
.addOnCompleteListener(this, new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
// [START_EXCLUDE]
updateUI(null);
// [END_EXCLUDE]
}
});
}
private void updateUI(@Nullable GoogleSignInAccount account) {
if (account != null) {
Log.d(TAG, account.getDisplayName());
} else {
Log.d(TAG, "account is null");
}
}
@Override
public void onStart() {
super.onStart();
// [START on_start_sign_in]
// 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);
// [END on_start_sign_in]
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Result returned from launching the Intent from GoogleSignInClient.getSignInIntent(...);
if (requestCode == RC_SIGN_IN) {
// The Task returned from this call is always completed, no need to attach
// a listener.
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
handleSignInResult(task);
}
}
private void handleSignInResult(Task<GoogleSignInAccount> completedTask) {
try {
GoogleSignInAccount account = completedTask.getResult(ApiException.class);
// Signed in successfully, show authenticated UI.
updateUI(account);
} catch (ApiException e) {
// The ApiException status code indicates the detailed failure reason.
// Please refer to the GoogleSignInStatusCodes class reference for more information.
Log.d(TAG, "signInResult:failed code=" + e.getStatusCode());
updateUI(null);
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.sign_in_button:
signIn();
break;
}
}
}
解决方案
您已经在 onStart() 回调方法中调用了getLastSignedInAccount方法。因此,当您使用 gmail 帐户成功登录后,每次进入此活动后,它都不会显示帐户选择对话框。所以,不用担心这个。
您可以通过以下代码注销您的用户。那么您将获得与第一次相同的登录模式。
private void signOut() {
mGoogleSignInClient.signOut()
.addOnCompleteListener(this, new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
// ...
}
});
}
推荐阅读
- redirect - 如果在请求中找到字符串,haproxy 重定向到新域,但保留并发送所有 URL 参数
- python - 我不知道如何处理这个 python 错误 TypeError: 'tuple' object is not callable
- r - 是否可以对输出 bigz 对象的函数进行矢量化?
- android - IKEv2 如何在没有原始套接字的 Android 上工作
- mongodb - 在 mongodb 聚合中的数组内查找嵌套对象字段
- django - 为 Django+React 项目设置部署,每个项目都使用 Nginx 在不同的端口上运行
- mysql - MySQL-将单个转义字符插入 MySQL JSON 字段
- python - 由 PyDub AudioSegment.split_on_silence() 产生的 .WAV 音频在使用 wave.open 读取时没有 RIFF ID
- reactjs - React hook useEffect 无法读取使用 firebase 的 firestore 实时数据更新的新 useState 值
- javascript - 在呈现页面后尝试从 JavaScript 函数访问模型数据 NodeJS EJS