添加Facebook按钮后,谷歌登录按钮无法正常工作

问题描述 投票:0回答:1

我有一个问题,我的应用程序中谷歌的登录按钮无法正常工作。这里的问题是因为我添加的facebook功能。当我从on create方法中删除此函数时,google登录工作正常,这是解决所有问题的方法:

FacebookSdk.sdkInitialize(getApplicationContext());
    setContentView(R.layout.activity_gesta_main);
    mCallbackManager = CallbackManager.Factory.create();
    LoginButton mLoginButton = (LoginButton) findViewById(R.id.login_button);

    mLoginButton.registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            String userLoginId = loginResult.getAccessToken().getUserId();
            Intent facebookIntent = new Intent(GestaMainActivity.this, Facebook_login.class);
            Profile mProfile = Profile.getCurrentProfile();
            String firstName = mProfile.getFirstName();
            String lastName = mProfile.getLastName();
            String userId = mProfile.getId().toString();
            String profileImageUrl = mProfile.getProfilePictureUri(96, 96).toString();

            facebookIntent.putExtra(PROFILE_USER_ID, userId);
            facebookIntent.putExtra(PROFILE_FIRST_NAME, firstName);
            facebookIntent.putExtra(PROFILE_LAST_NAME, lastName);
            facebookIntent.putExtra(PROFILE_IMAGE_URL, profileImageUrl);
            startActivity(facebookIntent);

        }

        @Override
        public void onCancel() {
        }

        @Override
        public void onError(FacebookException error) {
        }

有没有办法解决这个问题?

这是我的主要活动:

public class GestaMainActivity extends AppCompatActivity implements GoogleApiClient.OnConnectionFailedListener{
private SignInButton btnLogin;
private GoogleSignInOptions googleSignInOptions;
private GoogleApiClient googleApiClient;
private ProgressDialog mConnectionProgressDialog;
private static final int REQUEST_CODE_LOGIN = 10;

public static final String PROFILE_GOOGLE = "PROFILE_FIRST_NAME";
public static final String PROFILE_GOOGLE_EMAIL = "PROFILE_LAST_NAME";
public static final String PROFILE_GOOGLE_IMAGE_URL = "PROFILE_IMAGE_URL";

LoginButton mLoginButton;
private CallbackManager mCallbackManager;
public static final String PROFILE_USER_ID = "USER_ID";
public static final String PROFILE_FIRST_NAME = "PROFILE_FIRST_NAME";
public static final String PROFILE_LAST_NAME = "PROFILE_LAST_NAME";
public static final String PROFILE_IMAGE_URL = "PROFILE_IMAGE_URL";


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_gesta_main);
    setTitle("Gesta App");

    googleSignInOptions = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN).requestEmail().requestProfile().build();
    googleApiClient = new GoogleApiClient.Builder(this).enableAutoManage(this, this)
            .addApi(Auth.GOOGLE_SIGN_IN_API, googleSignInOptions).addApi(Plus.API).build();

    btnLogin = (SignInButton) findViewById(R.id.signin_button_google);
    btnLogin.setSize(SignInButton.SIZE_STANDARD);
    btnLogin.setScopes(googleSignInOptions.getScopeArray());

    btnLogin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent signin = Auth.GoogleSignInApi.getSignInIntent(googleApiClient);
            startActivityForResult(signin, REQUEST_CODE_LOGIN);
        }
    });

    FacebookSdk.sdkInitialize(getApplicationContext());
    setContentView(R.layout.activity_gesta_main);
    mCallbackManager = CallbackManager.Factory.create();
    LoginButton mLoginButton = (LoginButton) findViewById(R.id.login_button);

    mLoginButton.registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            String userLoginId = loginResult.getAccessToken().getUserId();
            Intent facebookIntent = new Intent(GestaMainActivity.this, Facebook_login.class);
            Profile mProfile = Profile.getCurrentProfile();
            String firstName = mProfile.getFirstName();
            String lastName = mProfile.getLastName();
            String userId = mProfile.getId().toString();
            String profileImageUrl = mProfile.getProfilePictureUri(96, 96).toString();

            facebookIntent.putExtra(PROFILE_USER_ID, userId);
            facebookIntent.putExtra(PROFILE_FIRST_NAME, firstName);
            facebookIntent.putExtra(PROFILE_LAST_NAME, lastName);
            facebookIntent.putExtra(PROFILE_IMAGE_URL, profileImageUrl);
            startActivity(facebookIntent);

        }

        @Override
        public void onCancel() {
        }

        @Override
        public void onError(FacebookException error) {
        }

    });
    mConnectionProgressDialog = new ProgressDialog(this);
    mConnectionProgressDialog.setMessage("Signing in...");}

protected void onActivityResult(int requestcode, int resultcode, Intent data)
{
    super.onActivityResult(requestcode,resultcode,data);

    mCallbackManager.onActivityResult(requestcode, resultcode, data);
    if (requestcode==REQUEST_CODE_LOGIN)
    {
        GoogleSignInResult googleSignInResult = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
        GoogleSignInAccount account = googleSignInResult.getSignInAccount();
        Person profile = Plus.PeopleApi.getCurrentPerson(googleApiClient);

        try {
            Intent sendData = new Intent(GestaMainActivity.this,Google_Gesta.class);
            String name, email, dpurl;
            name = account.getDisplayName();
            email = account.getEmail();
            dpurl = account.getPhotoUrl() != null ? account.getPhotoUrl().toString() : null;
            sendData.putExtra(PROFILE_GOOGLE,name);
            sendData.putExtra(PROFILE_GOOGLE_EMAIL,email);
            sendData.putExtra(PROFILE_GOOGLE_IMAGE_URL,dpurl);

            startActivity(sendData);

        }catch (Exception e)
        {
            Toast.makeText(GestaMainActivity.this,e.getMessage(),Toast.LENGTH_SHORT).show();
        }

    }else{
        Toast.makeText(GestaMainActivity.this,"Login Failed",Toast.LENGTH_LONG);
    }
}


@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

}

@Override
protected void onResume() {
    super.onResume();

    AppEventsLogger.activateApp(this);

}

@Override
protected void onPause() {
    super.onPause();
    AppEventsLogger.deactivateApp(this);
}
android facebook
1个回答
0
投票

Plus.PeopleApi.getCurrentPerson(googleApiClient);现已弃用。您可以从中获取所有信息

GoogleSignInAccount account = googleSignInResult.getSignInAccount();

从这里

© www.soinside.com 2019 - 2024. All rights reserved.