2016-01-22 42 views
0

我正在通过Udacity课程将Google Sign-In集成到Android应用程序中。我们试图通过onConnected()中的.getCurrentOerson()获取当前用户的详细信息,但.getCurrentOerson()已被弃用。.getCurrentPerson(mGoogleApiClient)在GooglePlayServices中弃用8.4

public class SignInAct extends Activity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener { 
GoogleApiClient mGoogleApiClient; 
Button signIn,signOut,revoke; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.signinlay); 
    mGoogleApiClient = buildApiClient(); 
} 
GoogleApiClient buildApiClient(){ 
    return new GoogleApiClient.Builder(this). 
      addConnectionCallbacks(this). 
      addOnConnectionFailedListener(this). 
      addApi(Plus.API, Plus.PlusOptions.builder().build()). 
      addScope(new Scope(Scopes.PROFILE)).build(); 
} 

    @Override 
public void onConnected(Bundle bundle) { 
    signIn.setEnabled(false); 
    signOut.setEnabled(true); 
    revoke.setEnabled(true); 

    Person currentUser = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient); 

}} 

现在得到displayName的方法是什么? 谢谢。

回答

2

public abstract Person getCurrentPerson (GoogleApiClient googleApiClient) documentation:

此方法已。

如果您仅使用People Api进行登录和获取身份,请改用GoogleSignInApi

From the GoogleSignInResult documentation:

public GoogleSignInAccount getSignInAccount()

返回GoogleSignInAccount反映用户的登录信息,如果登录成功完成;或失败时为空。

From the GoogleSignInAccount documentation:

public String getDisplayName()

返回的登录的用户,如果你建立你的配置与新GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)}或requestProfile开始()配置的显示名称;否则为空。不保证即使在配置时也适用于所有用户。

+0

谢谢你分享的解决方案,该应用程序正在编译,但它没有登录。请看看这个pastebin [链接](http://pastebin.com/x6WdSeVA),并指导我在哪里我可能会出错。当我点击SignIn按钮时,我总是得到这个输出 - 01-23 18:41:50.914 22617-22617/com.authorwjf D/SignInActivity:handleSignInResult:false。我已经包含了json配置文件,并在我的字符串值xml中添加了oauth客户端id。 – gpd