Android--Facebook登入

來源:互聯網
上載者:User

標籤:des   android   style   blog   http   io   color   os   ar   

1、Layout檔案添加Facebook的LoginBurtton

1 <com.facebook.widget.LoginButton2             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        }            }

 

Android--Facebook登入

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.