Facebook third-party login integration in Android

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.