Pages

Tuesday, May 6, 2014

FB Login

Declare

private boolean isFacebookLogin = false;
private Session.StatusCallback statusCallback = new SessionStatusCallback();
String name,email,bdate,contact;
private ProgressDialog mProgressDialog = null;
ProgressDialog pd;

RelativeLayout rl;

//OnCreate()

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));
            }
        }
        getFBUserData(session);

Login Click

call  onClickLogin()

@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 onClickLogin() {
        Session session = Session.getActiveSession();
        if (!session.isOpened() && !session.isClosed()) {
            session.openForRead(new Session.OpenRequest(this).setCallback(statusCallback));
            Toast.makeText(Login.this, "sucess", 10).show();
           // getFBUserData(session);
        } else {
            Session.openActiveSession(this, true, statusCallback);
            Toast.makeText(Login.this, "sucess", 10).show();
           // getFBUserData(session);
        }
    }

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

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

if (session.isOpened()) {
if (Login.this != null) {
mProgressDialog = ProgressDialog.show(Login.this, "", "Please Wait...");
}

Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {

public void onCompleted(GraphUser user, Response response) {
if (user != null) {
if (mProgressDialog.isShowing()) {
mProgressDialog.dismiss();
}

String link = user.getLink();
name = user.getName();
email = user.getUsername();
bdate = user.getBirthday();


isFacebookLogin = true;

if (link.length() != 0) {

SharedPreferences savedData = getSharedPreferences("Dompanion", 0);

SharedPreferences.Editor editor = savedData.edit();



editor.putString("email", email);
editor.putString("name", name);

editor.putInt("logged_in", 2);
editor.putString("dom", "A");
editor.commit();

Intent i = new Intent(Login.this,MainActivity.class);
startActivity(i);

}


}
}
});
}

Menifest.xml

<meta-data
            android:name="com.facebook.sdk.ApplicationId"

            android:value="@string/client_app_id" />

Download

    

No comments:

Post a Comment