2014-01-30 64 views
0

我正在開發一個項目。在第一次活動中,我使用Facebook登錄並記錄了新的活動啓動之後。在哪裏我想從Facebook註銷。如何在第二次活動中從Facebook註銷。 PLZ幫助我。Facebook從另一活動註銷

回答

0

嘗試這樣:下面的代碼

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

它不適用於第二項活動。 – Amardeepvijay

+0

你在第二次活動中得到了什麼錯誤..從我的理解你需要將facebook會話作爲參數傳遞給下一個活動 –

+0

如何將fb會話從一個活動傳遞給另一個活動?我正在使用sp和access_token。 – Amardeepvijay

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(); 
      } 
     }); 
     } 
    } 
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 
    } 
} 
0

把這個FacebookSdk.sdkInitialize(getApplicationContext());然後在按鈕單擊事件上寫下面的代碼:

LoginManager.getInstance()。logOut();

適合我。

把它從https://stackoverflow.com/a/31759037/7577192