android - 如何将 Google 登录对象传递给另一个 Android Activity

标签 android android-intent android-activity

我在我的 Android 应用中集成了 Google Sign-In。现在我想在另一个 Activity 中实现注销操作(注销按钮),相对于存在登录按钮的 Activity 。 正在关注google tutorial ,为了注销用户,我必须调用在登录阶段获得的 GoogleSignInClient 对象。

如何将此对象传递给 Intent 中的另一个 Activity ?这是应该遵循的正确程序,还是有其他方法可以实现这一目标?

这是我的代码:

FirstActivity.java

private GoogleSignInClient mGoogleSignInClient;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Configure sign-in to request the user's ID, email address, and basic
    // profile. ID and basic profile are included in DEFAULT_SIGN_IN.
    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN).requestEmail().build();

    // Build a GoogleSignInClient with the options specified by gso.
    mGoogleSignInClient = GoogleSignIn.getClient(this, gso);

    // Set the dimensions of the sign-in button.
    SignInButton signInButton = findViewById(R.id.sign_in_button);
    signInButton.setSize(SignInButton.SIZE_STANDARD);

    signInButton.setOnClickListener(this);
}

@Override
public void onClick(View view) {
    switch (view.getId()) {
        case R.id.sign_in_button:
            Log.d(LOG_TAG, "Google button clicked!");
            signIn();
            break;
    }
}
private void updateUI(@Nullable GoogleSignInAccount account) {
    if(account != null){
        Intent intent = new Intent(this, SecondActivity.class);
        String email = account.getEmail();
        intent.putExtra(Intent.EXTRA_EMAIL, email);

        startActivity(intent);
    }
}

最佳答案

无需传递对象,因为您可以使用 GoogleSignIn.getLastSignedInAccount请求当前登录用户的个人资料信息的方法。

更多信息:

https://developers.google.com/identity/sign-in/android/people

关于android - 如何将 Google 登录对象传递给另一个 Android Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49135812/

相关文章:

android - 非常简单的代码,但是使用 Fragment 时出现错误 "Activity has been destroyed"

java - 使用 pinterest 应用程序打开 pinterest 链接(如果存在)- android

android - 显示一个List的所有产品

java - 带有选择器的 FrameLayout 前景

android - Google API - YouTube - GuideCategories : list (Android)

android - 近场通信。扫描 NDEF 消息时启动 Activity

android - 给定资源值的 <color> 无效

android - 如何模拟 Android NFC 标签对象进行单元测试

android - onBackPressed() 方向问题

android - 单击后 Alertdialog 保持 Activity 状态