我如何通过未弃用的方法让Android从Google帐户获取用户性别
这是我的代码
package .activities;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.google.android.gms.auth.api.signin.GoogleSignIn;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.gms.auth.api.signin.GoogleSignInClient;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.common.Scopes;
import com.google.android.gms.common.SignInButton;
import com.google.android.gms.common.api.ApiException;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.Scope;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import .R;
public class LoginByGoogleActivity extends FragmentActivity implements
View.OnClickListener {
private static final String TAG = "LoginByGoogle";
private SignInButton mSignInButton;
private Button mSignOutButton;
private Button mRevokeButton;
private TextView mStatus;
ImageView imgView;
GoogleSignInClient mGoogleSignInClient;
private ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_by_google);
// Get references to all of the UI views
mSignInButton = (SignInButton) findViewById(R.id.sign_in_button);
mSignOutButton = (Button) findViewById(R.id.sign_out_button);
mRevokeButton = (Button) findViewById(R.id.revoke_access_button);
mStatus = (TextView) findViewById(R.id.statuslabel);
imgView = (ImageView) findViewById(R.id.preview);
progressBar = findViewById(R.id.progressbar);
progressBar.setVisibility(View.GONE);
// Add click listeners for the buttons
mSignInButton.setOnClickListener(this);
mSignOutButton.setOnClickListener(this);
mRevokeButton.setOnClickListener(this);
// 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)
.requestProfile()
.requestEmail()
.build();
// Build a GoogleSignInClient with the options specified by gso.
mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.sign_in_button:
mStatus.setText("Signing In");
signIn();
break;
case R.id.sign_out_button:
signOut();
break;
case R.id.revoke_access_button:
revokeAccess();
break;
}
}
private void signOut() {
mGoogleSignInClient.signOut()
.addOnCompleteListener(this, new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
onSignedOut();
// ...
Log.e(TAG, "You logged out");
}
});
}
private void revokeAccess() {
mGoogleSignInClient.revokeAccess()
.addOnCompleteListener(this, new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
Log.e(TAG, "You revoke access");
}
});
}
@Override
protected void onStart() {
super.onStart();
GoogleSignInAccount acct = GoogleSignIn.getLastSignedInAccount(this);
if (acct != null) {
if(acct.getIdToken() != null){
Log.d(TAG, " On Start ACtion " + acct.getIdToken());
}
onSignedIn();
updateUI(acct);
}
}
private void updateUI(GoogleSignInAccount account) {
mStatus.setText(account.getEmail()+" " + account.getDisplayName()+" " + account.getId());
if(account.getPhotoUrl() != null){
Utilities.loadUserPhoto(account.getPhotoUrl().toString(), imgView, 1, LoginByGoogleActivity.this, 0);
Log.e(TAG, account.getPhotoUrl().toString());
}
}
private void signIn() {
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
startActivityForResult(signInIntent, RC_SIGN_IN);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Result returned from launching the Intent from GoogleSignInClient.getSignInIntent(...);
if (requestCode == RC_SIGN_IN) {
// The Task returned from this call is always completed, no need to attach
// a listener.
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
handleSignInResult(task);
}
}
private void handleSignInResult(Task<GoogleSignInAccount> completedTask) {
if (completedTask.isSuccessful()) {
// There's immediate result available.
GoogleSignInAccount account = completedTask.getResult();
updateUI(account);
onSignedIn();
} else {
// There's no immediate result ready, displays some progress indicator and waits for the
// async callback.
showProgressIndicator();
try {
hideProgressIndicator();
onSignedIn();
GoogleSignInAccount account = completedTask.getResult(ApiException.class);
// Signed in successfully, show authenticated UI.
updateUI(account);
} catch (ApiException e) {
// The ApiException status code indicates the detailed failure reason.
// Please refer to the GoogleSignInStatusCodes class reference for more information.
// You can get from apiException.getStatusCode() the detailed error code
// e.g. GoogleSignInStatusCodes.SIGN_IN_REQUIRED means user needs to take
// explicit action to finish sign-in;
// Please refer to GoogleSignInStatusCodes Javadoc for details
Log.w(TAG, "signInResult:failed code=" + e.getStatusCode());
updateUI(null);
}
}
}
private void hideProgressIndicator() {
progressBar.setVisibility(View.VISIBLE);
}
private void showProgressIndicator() {
progressBar.setVisibility(View.GONE);
}
private void onSignedOut() {
// Update the UI to reflect that the user is signed out.
mSignInButton.setEnabled(true);
mSignOutButton.setEnabled(false);
mRevokeButton.setEnabled(false);
//imgView.setImageResource(R.drawable.mansmall);
Utilities.loadUserPhoto("", imgView, 1, LoginByGoogleActivity.this, 0);
mStatus.setText("Signed out");
}
private void onSignedIn(){
mSignInButton.setEnabled(false);
mSignOutButton.setEnabled(true);
mRevokeButton.setEnabled(true);
}
}
我如何使用未使用过的方法(如Plus和People)从Android帐户中获取Android用户性别信息 我用Google搜索的越来越多,有很多方法,但已弃用.. 我看着这个 http://coderzpassion.com/android-working-latest-google-plus-login-api/ 和 How to get profile like gender from google signin in Android? 和别的 但无法使用不推荐使用的方法来(性别) 预先感谢...