How can my app know when a user logs out of their native Android app on Facebook?

How can my app know when a user logs out of their native Facebook app?

I am using below code to log out Facebook in my application. And it works fine.

class LogoutTask extends AsyncTask<Void, Void, Void> {

       @Override
       protected Void doInBackground(Void... params) {
            if (facebook.isSessionValid()) {
                try {
                    facebook.logout(getApplicationContext());
                    editLoginStatus(false);
                    SharedPreferences.Editor editor = mPrefs.edit();
                    editor.putString("access_token", null);
                    editor.putLong("access_expires",
                            facebook.getAccessExpires());
                    editor.commit();

                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {

            super.onPostExecute(result);
        }

    }

      

But the problem occurs when the user logs out of their native Facebook app and then presses the logout button from my app.

It shows me the following error

 java.lang.RuntimeException: An error occured while executing doInBackground()
 at android.os.AsyncTask$3.done(AsyncTask.java:299)
 at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
 at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
 at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
 at java.util.concurrent.FutureTask.run(FutureTask.java:137)
 at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
 at java.lang.Thread.run(Thread.java:856)
 Caused by: java.lang.IllegalArgumentException: Invalid context argument
 at android.webkit.CookieSyncManager.createInstance(CookieSyncManager.java:86)
 at com.facebook.internal.Utility.clearCookiesForDomain(Utility.java:319)
 at com.facebook.internal.Utility.clearFacebookCookies(Utility.java:343)
 at com.facebook.Session.closeAndClearTokenInformation(Session.java:798)
 at com.facebook.android.Facebook.logoutImpl(Facebook.java:665)
 at com.facebook.android.Facebook.logout(Facebook.java:642)
 at com.example.animation.StartLoginActivity$LogoutTask.doInBackground(StartLoginActivity.java:328)
 at com.example.animation.StartLoginActivity$LogoutTask.doInBackground(StartLoginActivity.java:1)
 at android.os.AsyncTask$2.call(AsyncTask.java:287)
 at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)

      

and i call this function below

if (facebook.getAccessToken() != null && facebook.isSessionValid() == true) {
    new LogoutTask().execute();
}

      

here accessToken I store locally in sharedPreference, so it won't be empty until I log out of my application, but I think that after logging out of my own FB application, it facebook.isSessionValid()

should return false while it returns true. So how can you check if the Facebook application is already logged out or not?

+3


source to share


3 answers


to destroy you, clear the session and token information so that when exiting native app .. it will clear its credentials

use this

  @Override 
  protected void onDestroy() {

   // TODO Auto-generated method stub
  super.onDestroy();
 Session session = Session.getActiveSession();
 session.closeAndClearTokenInformation();


  }

      



OR

   public void logoutFromFacebook() {
   mAsyncRunner.logout(this, new RequestListener() {
    @Override
    public void onComplete(String response, Object state) {
        Log.d("Logout from Facebook", response);
        if (Boolean.parseBoolean(response) == true) {
            // User successfully Logged out
        }
    }

    @Override
    public void onIOException(IOException e, Object state) {
    }

    @Override
    public void onFileNotFoundException(FileNotFoundException e,
            Object state) {
    }

    @Override
    public void onMalformedURLException(MalformedURLException e,
            Object state) {
    }

    @Override
    public void onFacebookError(FacebookError e, Object state) {
    }
});
}

      

try this it works for me .. so it will solve your problem as well

+1


source


Try to check if you have a Facebook account on the system.

AccountManager accountManager = AccountManager.get(context);
Account[] facebookAccounts = accountManager.getAccountsByType("com.facebook.auth.login");
if (facebookAccounts.length > 0) {
    facebook.logout(getApplicationContext());
    ...
}

      



Yes, the code contains a hard-coded account type, which is a little unreliable due to possible changes in the native application, although the likelihood of such changes is very small.

And don't forget to add GET_ACCOUNTS to your manifest .

+1


source


Hmmmm ... This can be tricky. I am assuming isSessionValid is offline from the FB API.

From your situation, it looks like you still need to have one FB call to check if the session is actually valid or not. So my suggestion based on your situation is to just skip the FB API call, for example /me

to check if the session is correct.

If the session is valid, you manually call the logout API, otherwise you know that the FB session has either expired or logged out.

Hope helps :)

0


source







All Articles