简体   繁体   English

在另一个活动中获取 google 登录帐户

[英]Get the google signed in account in another activity

I read some posts about it but i didn't find the right way for this.我读了一些关于它的帖子,但我没有找到正确的方法。 So here is the problem:所以这里是问题:

I have used google signin in my login activity , everything works fine.我在login activity中使用了google signin ,一切正常。 The user goes to another activity , then to a third one and then in another one.用户进入another activity ,然后进入第三个活动,然后进入另一个活动。 In the fourth activity i need the google signed user (actually is an activity to use Google Play Services, achievements etc.) What is the best and shorter way to get the signed in user?fourth activity中,我需要 google 登录用户(实际上是使用 Google Play 服务、成就等的活动)。获得登录用户的最佳和更短的方法是什么? Do i have to make ALL the login processes again?我是否必须再次进行所有登录过程?

UPDATE:更新:

As mentioned updated code in my activity as follow:正如在我的活动中提到的更新代码如下:

@Override
protected void onStart() {
    super.onStart();        
    if (!isSignedIn()) {
        signInSilently();
    }

And

private boolean isSignedIn() {
    return GoogleSignIn.getLastSignedInAccount(this) != null;
}
private void signInSilently() {
    Log.d(TAG, "signInSilently()");

    mGoogleSignInClient.silentSignIn().addOnCompleteListener(this,
            new OnCompleteListener<GoogleSignInAccount>() {
                @Override
                public void onComplete(@NonNull Task<GoogleSignInAccount> task) {
                    if (task.isSuccessful()) {
                        Log.d(TAG, "signInSilently(): success");
                        onConnected(task.getResult());
                    } else {
                        Log.d(TAG, "signInSilently(): failure", task.getException());
                        onDisconnected();
                    }
                }
            });

as i dont want to have a new login (buttons etc).因为我不想重新登录(按钮等)。 First i think that user should be connected.首先,我认为应该连接用户。 But even if he is disconnected, signInSilently() should connect again the user.但即使他断开连接,signInSilently() 也应该再次连接用户。

My final problem is that i get a null object exception in:我的最后一个问题是我得到一个 null object 异常:

mLeaderboardsClient.submitScore(getString(R.string.leaderboard_leaderboard),



private void onConnected(GoogleSignInAccount googleSignInAccount) {
    Log.d(TAG, "onConnected(): connected to Google APIs");

    mAchievementsClient = Games.getAchievementsClient(this, googleSignInAccount);
    mLeaderboardsClient = Games.getLeaderboardsClient(this, googleSignInAccount);
    mEventsClient = Games.getEventsClient(this, googleSignInAccount);
    mPlayersClient = Games.getPlayersClient(this, googleSignInAccount);

as i can understand because of NO GOOGLE user.我可以理解,因为没有 GOOGLE 用户。 What else i have to do so get my google signedin user?我还需要做什么才能让我的谷歌登录用户?

You can use this function to check if the user is logged in or not.您可以使用此 function 来检查用户是否已登录。

private boolean isSignedIn() {
  return GoogleSignIn.getLastSignedInAccount(context) != null;
}

https://developers.google.com/android/reference/com/google/android/gms/auth/api/signin/GoogleSignIn https://developers.google.com/android/reference/com/google/android/gms/auth/api/signin/GoogleSignIn

public static GoogleSignInAccount getLastSignedInAccount (Context context)公共 static GoogleSignInAccount getLastSignedInAccount(上下文上下文)

Gets the last account that the user signed in with.获取用户登录的最后一个帐户。

Returns: GoogleSignInAccount from last known successful sign-in.返回:上次成功登录的 GoogleSignInAccount。 If user has never signed in before or has signed out / revoked access, null is returned.如果用户之前从未登录或已注销/撤销访问权限,则返回 null。

If it returns null just relogin the user.如果它返回 null 只需重新登录用户。 that's all.就这样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Google Play游戏服务-下次活动未登录 - Google Play Game Services - not signed in on next activity Firebase - 获取登录用户的详细信息并在另一个活动中注销用户 - Firebase - Get signed in user's detail and sign out the user in another activity 登录到Google和Facebook帐户后如何继续进行Android Studio中的其他活动 - How to continue to another activity in android studio after login to google and facebook account 获取 Google 签名的用户信息 - Get Google signed user information Google帐户获取令牌 - Google account get Token 如何在另一个活动中获取Google Map实时位置 - How to get google map real time location in another activity 以编程方式在Android中启动“添加Google帐户”活动 - Programmatically starting the 'Add Google Account' activity in Android 在Google Play商店中上传已签名的apk时使用其他帐户 - Using other account when uploading signed apk in google play store 使用设备的已登录Google帐户发送电子邮件 - Use device's signed-in Google account to send email 如何检查用户是否已经通过 gmail 或谷歌帐户登录? - How to check if user is already signed in via gmail or google account?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM