Facebook(一)自定义登录按钮,登陆完毕后清除缓存的Session信

一、要实现的功能

Facebook自带的LoginButton,,点击授权完成后,登录按钮变成登出按钮,但是不是我想要的功能,因为缓存信息还在,下次登录的时候都不用授权而是直接就进来了,这样想要切换用户都不能。

所以我根据Facebook提供的Demo自定研究了一个适合自己需求的Demo,要求使用自定义的登录按钮进行登录操作并能够及时的清除缓存信息,这样下次进来的时候虽然需要重新登录授权,但是确是达到了切换用户的需求。

图片:

第一次进入应用:

点击按钮

授权完毕:

第二次进入:

二、代码如下:

1.布局文件

main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android=""android:id="@+id/fragmentContainer"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><Buttonandroid:id="@+id/btn"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="login with facebook" ></Button></LinearLayout>activity_second.xml<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android=""android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><TextViewandroid:id="@+id/userinfo"android:layout_width="match_parent"android:layout_height="wrap_content" /></LinearLayout>2.关键类

MainActivity.java

点击进入登录授权的界面

/** * Copyright 2010-present Facebook. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package com.facebook.samples.switchuser;import android.content.Intent;import android.os.Bundle;import android.support.v4.app.FragmentActivity;import android.view.ContextMenu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import com.facebook.AppEventsLogger;import com.facebook.Request;import com.facebook.Response;import com.facebook.Session;import com.facebook.SessionLoginBehavior;import com.facebook.SessionState;import com.facebook.SharedPreferencesTokenCachingStrategy;import com.facebook.model.GraphUser;public class MainActivity extends FragmentActivity implements OnClickListener {private static final String TOKEN_CACHE_NAME_KEY = "TokenCacheName";private Slot currentSlot;private Session session;private Session.StatusCallback sessionStatusCallback;public static final String TAG = "SettingsFragment";private Button btn;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);sessionStatusCallback = new Session.StatusCallback() {@Overridepublic void call(Session session, SessionState state, Exception exception) {onSessionStateChange(session, state, exception);}};SessionLoginBehavior loginBehavior = SessionLoginBehavior.SUPPRESS_SSO;currentSlot = new Slot(this, loginBehavior);if (savedInstanceState != null) {SharedPreferencesTokenCachingStrategy restoredCache = new SharedPreferencesTokenCachingStrategy(this, savedInstanceState.getString(TOKEN_CACHE_NAME_KEY));session = Session.restoreSession(this, restoredCache, sessionStatusCallback, savedInstanceState);}btn = (Button) this.findViewById(R.id.btn);btn.setOnClickListener(this);}/*** * Fragment的方法,创建上下文菜单的时候调用 */@Overridepublic void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo) {super.onCreateContextMenu(menu, view, menuInfo);getMenuInflater().inflate(R.menu.context_settings, menu);}/** * 清除Cache的方法 * * @param position */private void clearCache() {if (currentSlot.getUserId() != null) {currentSlot.clear();notifySlotChanged();}}/** * 自定义授权按钮 */@Overridepublic void onClick(View arg0) {notifySlotChanged();}@Overrideprotected void onSaveInstanceState(Bundle outState) {super.onSaveInstanceState(outState);if (currentSlot != null) {outState.putString(TOKEN_CACHE_NAME_KEY, currentSlot.getTokenCacheName());}Session.saveSession(session, outState);}@Overrideprotected void onResume() {super.onResume();if (session != null) {session.addCallback(sessionStatusCallback);}// Call the 'activateApp' method to log an app event for use in// analytics and advertising reporting. Do so in// the onResume methods of the primary Activities that an app may be// launched into.AppEventsLogger.activateApp(this);}@Overrideprotected void onPause() {super.onPause();if (session != null) {session.removeCallback(sessionStatusCallback);}// Call the 'deactivateApp' method to log an app event for use in// analytics and advertising// reporting. Do so in the onPause methods of the primary Activities// that an app may be launched into.AppEventsLogger.deactivateApp(this);}@Overridepublic void onActivityResult(int requestCode, int resultCode, Intent data) {super.onActivityResult(requestCode, resultCode, data);if (session != null) {session.onActivityResult(this, requestCode, resultCode, data);}}// 授权后的回调的方法private void onSessionStateChange(Session session, SessionState state, Exception exception) {if (state.isOpened()) {// Log in just happened.fetchUserInfo();} else if (state.isClosed()) {// Log out just happened. Update the UI.}}private void fetchUserInfo() {if (session != null && session.isOpened()) {Request request = Request.newMeRequest(session, new Request.GraphUserCallback() {@Overridepublic void onCompleted(GraphUser user, Response response) {if (response.getRequest().getSession() == session) {if (user != null) {Slot s = currentSlot;if (s != null) {s.update(user);Intent intent = new Intent(MainActivity.this, SecondActivity.class);intent.putExtra("userinfo", "id:" + s.getUserId() + ",username" + s.getUserName() + ",gender" + s.getGender());s.clear();startActivity(intent);}}}}});request.executeAsync();}}private void handleSlotChange(Slot newSlot) {if (session != null) {session.close();session = null;}// 对用户进行授权操作,缓存信息保存到Session中if (newSlot != null) {session = new Session.Builder(this).setTokenCachingStrategy(newSlot.getTokenCache()).build();session.addCallback(sessionStatusCallback);Session.OpenRequest openRequest = new Session.OpenRequest(this);openRequest.setLoginBehavior(newSlot.getLoginBehavior());openRequest.setRequestCode(Session.DEFAULT_AUTHORIZE_ACTIVITY_CODE);session.openForRead(openRequest);}}private void notifySlotChanged() {handleSlotChange(currentSlot);}}SecondActivity.java便是不再存在着任何我曾经对你有过的希望。

Facebook(一)自定义登录按钮,登陆完毕后清除缓存的Session信

相关文章:

你感兴趣的文章:

标签云: