Skip to content

Instantly share code, notes, and snippets.

@josecostamartins
Forked from timothy1ee/LoginActivity.java
Last active August 29, 2015 14:11
Show Gist options
  • Save josecostamartins/81a2999abeb210a80dc6 to your computer and use it in GitHub Desktop.
Save josecostamartins/81a2999abeb210a80dc6 to your computer and use it in GitHub Desktop.
import java.util.Arrays;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import com.facebook.Request;
import com.facebook.Response;
import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.UiLifecycleHelper;
import com.facebook.model.GraphUser;
import com.facebook.widget.LoginButton;
public class LoginActivity extends Activity {
private UiLifecycleHelper uiHelper;
private Session.StatusCallback callback = new Session.StatusCallback() {
@Override
public void call(Session session, SessionState state, Exception exception) {
onSessionStateChange(session, state, exception);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
uiHelper = new UiLifecycleHelper(this, callback);
uiHelper.onCreate(savedInstanceState);
LoginButton button = (LoginButton) findViewById(R.id.btnFacebookLogin);
button.setReadPermissions(Arrays.asList("basic_info","email"));
}
private void onSessionStateChange(Session session, SessionState state, Exception exception) {
if (session != null && session.isOpened()) {
Log.d("DEBUG", "facebook session is open ");
// make request to the /me API
Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
// callback after Graph API response with user object
@Override
public void onCompleted(GraphUser user, Response response) {
if (user != null) {
Log.d("DEBUG", "email: " + user.asMap().get("email").toString());
}
}
});
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.login, menu);
return true;
}
@Override
public void onResume() {
super.onResume();
uiHelper.onResume();
}
@Override
public void onPause() {
super.onPause();
uiHelper.onPause();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
uiHelper.onActivityResult(requestCode, resultCode, data);
}
@Override
public void onDestroy() {
super.onDestroy();
uiHelper.onDestroy();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
uiHelper.onSaveInstanceState(outState);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment