1, first of all to download the FaceBook SDK Android version;
2, import facebooksdk as the library into their own projects
3. Register a test app on Facebook
4, Configuration androidmanifest.xml:
<activity android:name= "com.facebook.LoginActivity"/> <meta-data android:name= " Com.facebook.sdk.ApplicationId "android:value=" @string/app_id "/>
In the string, add:
<string name= "app_id" >1548440642074664</string>
5. Add Facebook Login button:
<com.facebook.widget.loginbutton android:id= "@+id/login_button" android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:layout_gravity= "Center_horizontal" android:layout_ Marginbottom= "30DP" android:layout_margintop= "30DP"/>
6. Add code to activity:
public class Mainactivity extends Activity {private static final String TAG = "mainactivity";p rivate uilifecyclehelper Uih Elper;private Session.statuscallback callback = new Session.statuscallback () {@Overridepublic void Call (Session session , sessionstate state,exception Exception) {Onsessionstatechange (Session, State, Exception);}}; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); uihelper = new Uilifecyclehelper (Mainactivity.this, callback); Uihelper.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main); Loginbutton Authbutton = (Loginbutton) Findviewbyid (R.id.login_button); Authbutton.setreadpermissions (Arrays.asList ("email", "user_likes", "User_status")); private void Onsessionstatechange (session session, sessionstate State,exception Exception) {if (state.isopened ()) { LOG.I (TAG, "logged in ..."); else if (state.isclosed ()) {log.i (TAG, "logged out ...");}} @Overridepublic void Onresume () {super.onresume ();//For scenarios where THe main activity is launched and user//session was not NULL, the session state change notification//could not be triggered. Trigger it ' s open/closed. Session session = Session.getactivesession (); if (session = null && (session.isopened () | | session.isclosed ())) { Onsessionstatechange (Session, Session.getstate (), null);} Uihelper.onresume ();} @Overridepublic void Onactivityresult (int requestcode, int resultcode, Intent data) {Super.onactivityresult ( Requestcode, ResultCode, data); Uihelper.onactivityresult (Requestcode, ResultCode, data);} @Overridepublic void OnPause () {super.onpause (); Uihelper.onpause ();} @Overridepublic void OnDestroy () {Super.ondestroy (); Uihelper.ondestroy ();} @Overridepublic void Onsaveinstancestate (Bundle outstate) {super.onsaveinstancestate (outstate); Uihelper.onsaveinstancestate (outstate);}}
If you want to get basic information about a user:
private void GetUserInfo () { String fqlquery = "Select Uid,name,email from user WHERE uid = Me ()"; Bundle params = new bundle (); Params.putstring ("Q", fqlquery); Request Request = new request (Session.getactivesession (), "/me", params, httpmethod.get, new Request.callback () {public void oncompleted (Response Response) { String str = response.tostring (); LOG.I (TAG, str); } ); Request.executebatchasync (request);}
Facebook third-party login integration in Android