2
votes

i have a buttonLoginLogout Button which is a facebook login button (i imported the facebook sdk into my project) and this is my methods in my mainActivity:

its takes from LoginUsingActivityActivity sample from facebook activity, i didnt see a GraphUser or oncompleted mehod, so how can i get the user detailes that connected via facebook...

i tried to add "void onCompleted" method (in the end as you can see), however i cant override it and i cant get the user details, and when i login to facebook (and the login succeed) it doesnt get to the oncompleted method...

how can i get the GraphUser that logged into facebook successfully?

Also, i added the onCompleted method here, it wasn't here and it says that it is not override another moethod..

thanks alot

Settings.addLoggingBehavior(LoggingBehavior.INCLUDE_ACCESS_TOKENS);

    Session session = Session.getActiveSession();
    if (session == null) {
        if (savedInstanceState != null) {
            session = Session.restoreSession(this, null, statusCallback, savedInstanceState);
        }
        if (session == null) {
            session = new Session(this);
        }
        Session.setActiveSession(session);
        if (session.getState().equals(SessionState.CREATED_TOKEN_LOADED)) {
            session.openForRead(new Session.OpenRequest(this).setCallback(statusCallback));
        }
    }

    updateView();
}

@Override
public void onStart() {
    super.onStart();
    Session.getActiveSession().addCallback(statusCallback);
}

@Override
public void onStop() {
    super.onStop();
    Session.getActiveSession().removeCallback(statusCallback);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    Session session = Session.getActiveSession();
    Session.saveSession(session, outState);
}

private void updateView() {
    Session session = Session.getActiveSession();
    if (session.isOpened()) {
        //button logout string
        buttonLoginLogout.setOnClickListener(new OnClickListener() {
            public void onClick(View view) { onClickLogout(); }
        });
    } else {
        //button log in string
        buttonLoginLogout.setOnClickListener(new OnClickListener() {
            public void onClick(View view) { onClickLogin(); }
        });
    }
}

private void onClickLogin() {
    Session session = Session.getActiveSession();
    if (!session.isOpened() && !session.isClosed()) {
        session.openForRead(new Session.OpenRequest(this).setCallback(statusCallback));
    } else {
        Session.openActiveSession(this, true, statusCallback);
    }
}

private void onClickLogout() {
    Session session = Session.getActiveSession();
    if (!session.isClosed()) {
        session.closeAndClearTokenInformation();
    }
}

private class SessionStatusCallback implements Session.StatusCallback {
    @Override
    public void call(Session session, SessionState state, Exception exception) {
        updateView();
    }

    //doesnt get here, why?????????
    public void onCompleted(GraphUser user, Response response) {


    }
}
1

1 Answers

8
votes

write this in a method:

final Session session = Session.getActiveSession();
    if (session != null && session.isOpened()) {
        // If the session is open, make an API call to get user data
        // and define a new callback to handle the response
        Request request = Request.newMeRequest(session, new Request.GraphUserCallback() {
            @Override
            public void onCompleted(GraphUser user, Response response) {
                // If the response is successful
                if (session == Session.getActiveSession()) {
                    if (user != null) {
                        user_ID = user.getId();//user id
                        profileName = user.getName();//user's profile name
                        userNameView.setText(user.getName());
                    }   
                }   
            }   
        }); 
        Request.executeBatchAsync(request);
    }  

and make sure that this method is called only after a successfully login. that mean u should call it from the onActivityResult.