How my app can know when user logout from its Andr

2019-02-15 08:03发布

How my app can know when user logout from its native Facebook app ?

I am using below code for Logging out from Facebook in my app. And it is working 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 problem is occurring when user logging out from its native Facebook app and then clicking on the logout button on my app.

It showing me 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 am calling this function as below

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

here accessToken I am storing locally in sharedPreference , so it won't be null as long as I am not logging out from my app , but I guess after logging out from native FB app facebook.isSessionValid() should return false while it is returning true .So how to check to know Facebook app is already logging out or not ?

3条回答
祖国的老花朵
2楼-- · 2019-02-15 08:32

Try to check if there's a Facebook account in 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 hard-coded account type, which is slightly unreliable due to possible changes in the native app, though the probability of such changes is very small.

And don't forget to add a GET_ACCOUNTS permission to the Manifest.

查看更多
孤傲高冷的网名
3楼-- · 2019-02-15 08:34

Hmmmm... It may be get tricky. I guess the isSessionValid is offline checking from the FB API.

From your situation, it looks like you still need to have a single call to the FB to check the session is really valid or not. So my suggestion based on your situation is to make simple FB API call such as /me just to check the validity of the session.

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

Hope helps :)

查看更多
做个烂人
4楼-- · 2019-02-15 08:39

on destroy you clear the session and token information so that if you logout from your native application .. 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 also resolve your issue also

查看更多
登录 后发表回答