如果未安装应用程序,Facebook登录不会启动第二个活动

时间:2016-12-06 20:07:02

标签: android facebook facebook-login

我试图在Android应用上实施Facebook登录。我按照Facebook开发人员指南进行操作但是如果没有安装Facebook应用程序,那么它会获取登录详细信息,登录并保持相同的活动,只显示注销按钮,如果我关闭应用程序并重新启动然后它完美运行精细。

public class MainActivity extends AppCompatActivity {

    LoginButton loginButton;
    CallbackManager callbackManager;


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        FacebookSdk.sdkInitialize(getApplicationContext());
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        isLoggedIn();

        loginButton = (LoginButton) findViewById(R.id.login_button);

        callbackManager = CallbackManager.Factory.create();

        LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                AccessToken   accessToken = loginResult.getAccessToken();
                Profile profile = Profile.getCurrentProfile();
                nextActivity(profile);

            }

            @Override
            public void onCancel() {

            }

            @Override
            public void onError(FacebookException error) {

            }
        });

        loginButton.setReadPermissions("user_friends");



    }

    protected void onActivityResult(int requestCode, int responseCode, Intent intent) {
        super.onActivityResult(requestCode, responseCode, intent);
        //Facebook login
        callbackManager.onActivityResult(requestCode, responseCode, intent);

    }

    public void isLoggedIn() {

        AccessToken accessToken = AccessToken.getCurrentAccessToken();
        if (accessToken!=null) {

            Profile profile=Profile.getCurrentProfile();

            nextActivity(profile);
        }
    }

    private void nextActivity(Profile profile) {
        if (profile != null) {                                                                                        Intent main = new Intent(MainActivity.this, Result.class);
            main.putExtra("Name", profile.getFirstName());
            main.putExtra("Surname", profile.getLastName());
            startActivity(main);
            finish();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

而不是使用LoginManager.getInstance().registerCallback()使用loginButton.registerCallback()

修改

在onSuccess内部有以下代码:

    @Override
    public void onSuccess(LoginResult loginResult) {
        AccessToken accessToken = loginResult.getAccessToken();
        if(Profile.getCurrentProfile() == null) {
                ProfileTracker profileTracker = new ProfileTracker() {
                        @Override
                        protected void onCurrentProfileChanged(Profile oldProfile, Profile currentProfile) {
                               this.stopTracking();
                               nextActivity(currentProfile);
                        }
                };
                profileTracker.startTracking();
        }else{
               Profile profile = Profile.getCurrentProfile();
               nextActivity(profile);
        }