Facebook从另一项活动退出

时间:2014-01-30 05:37:38

标签: android facebook-sdk-3.0

我正在开展一个项目。在第一个活动中,我正在使用Facebook登录并记录新的活动启动。我想从Facebook注销的地方。如何在第二次活动中从facebook注销。请帮助我。

4 个答案:

答案 0 :(得分:2)

尝试这样做对我有用。 //从facebook退出

public static void logoutFromFB(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
    }
}

答案 1 :(得分:0)

尝试这样的事情:

if (Session.getActiveSession() != null) {
    Session.getActiveSession().closeAndClearTokenInformation();
}
Session.setActiveSession(null);

答案 2 :(得分:0)

使用以下代码。

FBLogout.setOnClickListener(new OnClickListener() {
    @Override
        public void onClick(View v) {
            clearApplicationData();
            FBHandler.getInstance(this).facebookLogout();
        }
});   


public void clearApplicationData() {
        File cache = getCacheDir();
        File appDir = new File(cache.getParent());
        if (appDir.exists()) {
            String[] children = appDir.list();
            for (String s : children) {
                if (!s.equals("lib") && !s.equals("files")) {
                    deleteDir(new File(appDir, s));
                    Log.i("TAG",
                            "**************** File /data/data/APP_PACKAGE/" + s
                                    + " DELETED *******************");
                }
            }
        }
    }

    public static boolean deleteDir(File dir) {
        if (dir != null && dir.isDirectory()) {
            String[] children = dir.list();
            for (int i = 0; i < children.length; i++) {
                boolean success = deleteDir(new File(dir, children[i]));
                if (!success) {
                    return false;
                }
            }
        }

        return dir.delete();
    } 

<强> FBHandler.java

public class FBHandler
{
    private static final String TAG  ="FBHandler";
    private static final String appKey = "your app key";

    private static Facebook facebook = null;

    private static FBHandler _instance = null;

    private static Context context = null;

    private static String[] permissions = null;


    private FBHandler()
    {

    }
    public static FBHandler getInstance(Context ctx)
    {
        if(ctx == null)
        {
            Log.d(TAG, "Error: Context is null, hence returning from the FBHandler");
            return null; 
        }

        context = ctx;
        if(_instance == null)
        {
            facebook = new Facebook(appKey); 
            _instance = new FBHandler();
            permissions = new String[]{"publish_stream","user_photos", "publish_checkins","email","publish_actions"};
        }

        return _instance;
    }


    public void doFbUserLogin(final Activity activity, final fbListner listner)
    {
        if(activity == null || listner == null)
        {
            Log.d(TAG, "Error: User Login activity/Listner is null");
            return;
        }

        facebook.authorize(activity, permissions , new DialogListener()
        {

            public void onComplete(Bundle values) 
            {
                printoCommon.showToastMsg(activity, "facebook Login is succeesfull");
                listner.onSuccess();                

            }

            public void onFacebookError(FacebookError e) 
            {
                printoCommon.showToastMsg(activity, e.getMessage());

            }

            public void onError(DialogError e) 
            {

                printoCommon.showToastMsg(activity, e.getMessage());
            }

            public void onCancel() 
            {
                printoCommon.showToastMsg(activity, "Facebook Login is cancelled");

            }

        });
    }

    public void facebookLogout() 
    {

        AsyncFacebookRunner fbAsyncRun =  new AsyncFacebookRunner(facebook);
        fbAsyncRun.logout(context, new AsyncFacebookRunner.RequestListener() 
        {

            public void onMalformedURLException(MalformedURLException e, Object state) 
            {
                printoCommon.showToastMsg((Activity) context, e.getMessage());

            }

            public void onIOException(IOException e, Object state) 
            {
                printoCommon.showToastMsg((Activity) context, e.getMessage());

            }

            public void onFileNotFoundException(FileNotFoundException e, Object state) 
            {
                printoCommon.showToastMsg((Activity) context, e.getMessage());

            }

            public void onFacebookError(FacebookError e, Object state) 
            {
                printoCommon.showToastMsg((Activity) context, e.getMessage());

            }

            public void onComplete(String response, Object state) 
            {
                printoCommon.showToastMsg((Activity)context, "You have logged out from facebook Successfully");
                ((Activity) context).finish();
            }
        });
      }
  }

答案 3 :(得分:0)

在你的oncreate上放置 FacebookSdk.sdkInitialize(getApplicationContext()); ,然后在按钮点击事件上写下面的代码:

<强> LoginManager.getInstance()退出();

适合我。

https://stackoverflow.com/a/31759037/7577192

获取