简体   繁体   中英

What is the best way to implement facebook login on Android?

I looked tutorial on facebook developers and it looks different with what I found on internet. Which one is the best way to implement facebook login? Also, where is the best place to learn facebook sdk for android, it seems the official one is not complete?

Facebook.developer use loginButton.setRegisterCallback();

 @Override public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.splash, container, false); loginButton = (LoginButton) view.findViewById(R.id.login_button); loginButton.setReadPermissions("user_friends"); // If using in a fragment loginButton.setFragment(this); // Other app specific specialization // Callback registration loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() { @Override public void onSuccess(LoginResult loginResult) { // App code } @Override public void onCancel() { // App code } @Override public void onError(FacebookException exception) { // App code } }); } 

programmerguru.com use UiLifecycleHelper which I dont find it on the official sites

 public class MainActivity extends ActionBarActivity {    // Create, automatically open (if applicable), save, and restore the    // Active Session in a way that is similar to Android UI lifecycles.    private UiLifecycleHelper uiHelper;    private View otherView;    private static final String TAG = "MainActivity";     @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        // Set View that should be visible after log-in invisible initially        otherView = (View) findViewById(R.id.other_views);        otherView.setVisibility(View.GONE);        // To maintain FB Login session        uiHelper = new UiLifecycleHelper(this, callback);        uiHelper.onCreate(savedInstanceState);    }         // Called when session changes    private Session.StatusCallback callback = new Session.StatusCallback() {        @Override        public void call(Session session, SessionState state,                Exception exception) {            onSessionStateChange(session, state, exception);        }    };         // When session is changed, this method is called from callback method    private void onSessionStateChange(Session session, SessionState state,            Exception exception) {        final TextView name = (TextView) findViewById(R.id.name);        final TextView gender = (TextView) findViewById(R.id.gender);        final TextView location = (TextView) findViewById(R.id.location);        // When Session is successfully opened (User logged-in)        if (state.isOpened()) {            Log.i(TAG, "Logged in...");            // make request to the /me API to get Graph user            Request.newMeRequest(session, new Request.GraphUserCallback() {                 // callback after Graph API response with user                // object                @Override                public void onCompleted(GraphUser user, Response response) {                    if (user != null) {                        // Set view visibility to true                        otherView.setVisibility(View.VISIBLE);                        // Set User name                        name.setText("Hello " + user.getName());                        // Set Gender                        gender.setText("Your Gender: "                                + user.getProperty("gender").toString());                        location.setText("Your Current Location: "                                + user.getLocation().getProperty("name")                                        .toString());                    }                }            }).executeAsync();        } else if (state.isClosed()) {            Log.i(TAG, "Logged out...");            otherView.setVisibility(View.GONE);        }    }         @Override    public void onActivityResult(int requestCode, int resultCode, Intent data) {        super.onActivityResult(requestCode, resultCode, data);         uiHelper.onActivityResult(requestCode, resultCode, data);        Log.i(TAG, "OnActivityResult...");    }         @Override    public void onResume() {        super.onResume();        uiHelper.onResume();    }     @Override    public void onPause() {        super.onPause();        uiHelper.onPause();    }     @Override    public void onDestroy() {        super.onDestroy();        uiHelper.onDestroy();    }     @Override    public void onSaveInstanceState(Bundle outState) {        super.onSaveInstanceState(outState);        uiHelper.onSaveInstanceState(outState);    } } 

As @Ming Li said, stick to the official tutorials . The reason why there are many different tutorials out there are because Facebook SDK has many different version and each version may come with different way to do things with Facebook. They change it if they come up with a better idea to do things with Facebook.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM