无法使用Facebook4j注销

时间:2013-12-16 10:05:56

标签: android facebook-graph-api facebook-oauth

我正在使用Facebook4j库将Facebook与我的Android应用程序集成。 我成功登录了https://github.com/roundrop/facebook4j-android-example的示例,但我没有找到任何关于如何注销的文档或示例。

有什么建议吗?

1 个答案:

答案 0 :(得分:-1)

您只需将以下代码添加到您的退出点击

即可
fbLogout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            clearApplicationData();
            FBHandler.getInstance(this).facebookLogout();
        }
    });

并创建如下所示的FBHandler类

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 doPrintoFbUserLogin(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");

        }

    });
}

@SuppressWarnings("static-access")
public void uploadUserPhoto(String graphAction, final Activity activity,
        Bundle params, uploadListner listner) {

    AsyncFacebookRunner fbAsyncRun = new AsyncFacebookRunner(facebook);
    params.putString(facebook.TOKEN, facebook.getAccessToken());
    fbAsyncRun.request("me/" + graphAction, params, "POST",
            new photoUploadListener(activity), null);
}

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