Created
June 15, 2013 23:05
-
-
Save timothy1ee/5789923 to your computer and use it in GitHub Desktop.
Sample login activity incorporating Facebook Android SDK
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
UiLifecycleHelper ?