如何在Android中检索我登录的Google帐户的名字和姓氏?

Ale*_*amo 6 android google-authentication

我有一个简单的google authentification应用程序,我想显示一条欢迎消息.如果电子邮件帐户为j ohnsmith@gmail.com,我想Toast"欢迎约翰·史密斯!" 我怎样才能做到这一点?

这是我的代码:

if (user == null) {
        startActivityForResult(AuthUI.getInstance().createSignInIntentBuilder().build(), SIGN_IN_REQUEST_CODE);
    } else {
        Toast.makeText(MainActivity.this, "Welcome " + userName, Toast.LENGTH_SHORT).show();
    }
Run Code Online (Sandbox Code Playgroud)

我试图使用此代码,但我只获取用户名,而不是名字和姓氏.

AccountManager manager = (AccountManager) getSystemService(ACCOUNT_SERVICE);
Account[] list = manager.getAccounts();
Run Code Online (Sandbox Code Playgroud)

提前致谢!

Ahm*_*idi 2

活动结果

public void onActivityResult(int requestCode, int resultCode, Intent result) {
    if (requestCode == GOOGLE_REQUEST_CODE) { // Google + callback
       handleSignInResult(Auth.GoogleSignInApi.getSignInResultFromIntent(result));
       }
}
Run Code Online (Sandbox Code Playgroud)

处理登录结果

private void handleSignInResult(GoogleSignInResult googleSignInResult) {
    if (googleSignInResult.isSuccess()) {
        GoogleSignInAccount acct = googleSignInResult.getSignInAccount();
        if (acct != null) {
            //get the data you need from GoogleSignInAccount
            }
        } else {
            Toast.makeText(context.getApplicationContext(), "error", Toast.LENGTH_SHORT).show();
        }
    }
Run Code Online (Sandbox Code Playgroud)

您可以在以下位置找到更多信息GoogleSignInAccounthttps://developers.google.com/android/reference/com/google/android/gms/auth/api/signin/GoogleSignInAccount