[英]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.