Android 应用程序与 Facebook 登录无法与已安装的 Facebook 应用程序一起使用

当我卸载Facebook应用程序但安装的Facebook应用程序时,此代码运行良好。我使用的是 Facebook SDK 4.0。

这是我的代码

package com.example.nhp04.gqfood;
import com.facebook.AccessToken;
import com.facebook.AccessTokenTracker;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.FacebookSdk;
import com.facebook.Profile;
import com.facebook.login.LoginResult;
import com.facebook.login.widget.LoginButton;



public class Login extends AppCompatActivity implements Animation.AnimationListener {

private String info = "";
private LoginButton loginButton;
private CallbackManager callbackManager;
private AccessTokenTracker tracker;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FacebookSdk.sdkInitialize(getApplicationContext());
        callbackManager = CallbackManager.Factory.create();
        setContentView(R.layout.activity_login);
loginButton = (LoginButton)findViewById(R.id.login_button);



loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            AccessToken accessToken = loginResult.getAccessToken();
            Profile profile = Profile.getCurrentProfile();
            info = ("User ID: " + 

    loginResult.getAccessToken().getUserId() + "\n" + "Auth Token: " + loginResult.getAccessToken().getToken());
                }

                @Override
                public void onCancel() {
                    info = ("Login attempt canceled.");
                }

                @Override
                public void onError(FacebookException e) {
                    info = ("Login attempt failed.");
                }
            });
            System.out.println(info);
            tracker = new AccessTokenTracker() {
            @Override
            protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken, AccessToken currentAccessToken) {

            }
        };
        tracker.startTracking();
    }
    }

此函数用于检查登录

public boolean isLoggedIn() {
    AccessToken accessToken = AccessToken.getCurrentAccessToken();
    return accessToken != null;
}

这在恢复和停止方法上

@Override
protected void onResume() {
    super.onResume();
    if (isLoggedIn()){
        Intent home = new Intent(this, home.class);
        startActivity(home);
    }
}

@Override
protected void onStop() {
    super.onStop();
    tracker.stopTracking();
    finish();
}

这是我的onActivityResult

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    callbackManager.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        Intent home = new Intent(this, home.class);
        startActivity(home);
    } else {
        Toast.makeText(getApplicationContext(), "Unable to login please check your internet connection",Toast.LENGTH_LONG).show();
    }
}

答案 1

你的代码在哪里。在你需要使用.用户以下代码:onActivityResult()onActivityResult()callbackmanager

 @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    callbackManager.onActivityResult(requestCode, resultCode, data);

}

以上将在片段/活动中工作。确保您有

1. facebook app installed on your testing device
2. In facebook developer account check whether you have mentioned 
- correct package name : refer your android project manifestfile.xml

- check that have you mentioned correct launcher class
- Check that you have given correct debug/release hash key

3. Cross check your facebook application id and that mentioned in your manifestfile.xml facebook meta data are same

在下面的代码更改中

创建你之后callbackmanagersetContentView(...);

将其更改为下面FacebookSdk.sdkInitialize(getApplicationContext()); AppEventsLogger.activateApp(this); setContentView(R.layout.activity_login); callbackManager = CallbackManager.Factory.create();

请记住,如果这是你的Facebook问题,那么你的问题就在于此,不要浪费时间搜索其他东西。同时将登录失败方法放在Facebook sdk的回调中。

如果您仍有问题,请发表评论


答案 2

您可以在Facebook应用程序中删除您的应用程序,就像您可以在go设置中打开Facebook应用程序一样>>帐户设置>>app>>youer应用程序>>删除。删除您的应用程序后,它将卸载您的应用程序,然后重新安装并检查使用Facebook登录是否正常工作。


推荐