在Android中使用Google SignIn API获取用户的名字和姓氏

时间:2016-05-02 08:45:22

标签: android google-signin

我正在开发应用并使用Google SignIn。在iOS Api中,我可以得到这样的名字和姓氏:

let GoogleUser = GIDSignIn.sharedInstance().currentUser
let firstName = googleAccount.profile.givenName
let lastName = googleAccount.profile.familyName

但是在Android API中,我发现只有getDisplayName()在一个String中返回用户的全名。我找不到任何只返回名字或姓氏的方法。

The Google SignIn API for Android site

谢谢:)

3 个答案:

答案 0 :(得分:4)

在android中也有,

getFamilyName()
getGivenName()

参考,

  

com.google.android.gms.plus.model.people.Person.Name

答案 1 :(得分:0)

试试这个

String fullname = "Sathish Kumar Jeyasankar";
String[] parts = fullname.split("\\s+");
String firstname = parts[0]; // Sathish
String middlename = parts[1]; //  Kumar
String lastname = parts[2]; //  Jeyasankar

答案 2 :(得分:0)

//签名后我们正在调用此函数     private void handleSignInResult(GoogleSignInResult result){

    //If the login succeed
    if (result.isSuccess()) {

        signInButton.setVisibility(View.GONE);
        //Getting google account
        GoogleSignInAccount acct = result.getSignInAccount();

        String name=acct.getDisplayName();

        String email=acct.getEmail();

        Log.d("namegoogle",name);
        Log.d("emailgoogle",email);
        edit_signin_emailid.setText(email);

        String fullname = acct.getDisplayName();
        String[] parts = fullname.split("\\s+");
        Log.d("Length-->",""+parts.length);
        if(parts.length==2) {
            String firstname = parts[0];
            String lastname = parts[1];
            Log.d("First-->", "" + firstname);
            Log.d("Last-->", "" + lastname);
            AppController.setStringPref("firstnamebook", firstname);
            AppController.setStringPref("lastnamebook", lastname);

            Log.d("FirstApp", "" + AppController.getStringPref("firstnamebook"));
            Log.d("LastApp", "" + AppController.getStringPref("lastnamebook"));
        }
        else if(parts.length==3){
            String firstname = parts[0];
            String middlename = parts[1];
            String lastname = parts[2];
            Log.d("First-->", "" + firstname);
            Log.d("Last-->", "" + lastname);
            AppController.setStringPref("firstnamebook", firstname);
            AppController.setStringPref("lastnamebook", lastname);
        }


    }
}