Integration of FaceBook third-party logon in Android

Source: Internet
Author: User

Integration of FaceBook third-party logon in Android

1. First, download the FaceBook SDK Android version;

2. Import FaceBookSDK to your project as Library

3. Register a test application on FaceBook

4. Configure AndroidManifest. xml:

                
   

Add the following to String:

 
  1548440642074664
 
5. Add the Facebook logon button:

 
 

6. Add code to the activity:

public class MainActivity extends Activity {private static final String TAG = "MainActivity";private UiLifecycleHelper uiHelper;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 is not null, the session state change notification// may not be triggered. Trigger it if 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 obtain basic user information:

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);}




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.