Android--Facebook Login with LoginButton

1、Layout文件添加Facebook的LoginBurtton

1 <com.facebook.widget.LoginButton
2             android:id="@+id/authButton"
3             android:layout_width="fill_parent"
4             android:layout_height="wrap_content"
5             android:layout_gravity="center_horizontal"
6             android:layout_marginLeft="45dp"
7             android:layout_marginRight="45dp"
8             android:layout_marginTop="10dp"
9             android:background="#FF3B5790" />

2、 创建MainFragment 类,并且继承Fragment 类,

重写onCreateView()方法  ;通过LoginButton设置权限 setReadPermissions()    更多权限访问 http://developers.facebook.com/docs/reference/login/#permissions

        重写 onSessionStateChange()方法  ,作用:当session状态改变时调用该方法,state.isOpened() 为 true 时,则证明登录成功

private UiLifecycleHelper uiHelper;
private LoginButton authButton;

 @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        uiHelper = new UiLifecycleHelper(getActivity(), callback);
        uiHelper.onCreate(savedInstanceState);
     authButton = (LoginButton) view.findViewById(R.id.authButton);
        authButton.setFragment(this);
        authButton.setLoginBehavior(SessionLoginBehavior.SUPPRESS_SSO);
        authButton.setReadPermissions(Arrays.asList("email", "user_likes", "user_status"));
    }



 @Override
    public void onResume()
    {
        super.onResume();
        Session session = Session.getActiveSession();
        if (session != null && (session.isOpened() || session.isClosed()))
        {
            onSessionStateChange(session, session.getState(), null);
        }
        uiHelper.onResume();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);
        uiHelper.onActivityResult(requestCode, resultCode, data);
 
    }

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

    private Session.StatusCallback callback = new Session.StatusCallback()
    {
        @Override
        public void call(Session session, SessionState state, Exception exception)
        {
            onSessionStateChange(session, state, exception);
        }
    };
 private void onSessionStateChange(Session session, SessionState state, Exception exception)
    {
        if (state.isOpened())
        {

            loginByFacebookFinished(session);

        }
        else if (state.isClosed())
        {
        }
    }



/**
*登陆成功
**/
private void loginByFacebookFinished(final Session session) { Request request = Request.newMeRequest(session, new GraphUserCallback() { @Override public void onCompleted(GraphUser user, Response response) { dismissLoadingDlg(); try { AccountInfo accountInfo = null; accountInfo = new AccountInfo(); accountInfo.setVip(false); accountInfo.setHasLogin(true); accountInfo.setToken(session.getAccessToken()); //用户信息 if (user != null) { accountInfo.setShowUserName(user.getName()); accountInfo.setPassword(user.getLink()); accountInfo.setLoginType(LoginType.FACEBOOK); accountInfo.setEmail(user.getProperty("email") + ""); accountInfo.setLoginName(user.getProperty("name") + ""); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); request.executeAsync(); }

3、退出登录

 /**
     * Logout From Facebook 
     */
    public  void callFacebookLogout(Context context) {
        Session session = Session.getActiveSession();
        if (session != null) {
            
            if (!session.isClosed()) {
                session.closeAndClearTokenInformation();
                //clear your preferences if saved
            }
        } else {
            
            session = new Session(context);
            Session.setActiveSession(session);
            
            session.closeAndClearTokenInformation();
            //clear your preferences if saved
        }
        
    }

 

posted @ 2014-10-29 14:40  crane13  阅读(1045)  评论(0编辑  收藏  举报