Android Facebook SDK在登录后不会调用回调 [英] Android facebook sdk does not call callback after login

查看:410
本文介绍了Android Facebook SDK在登录后不会调用回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

帮助

我试图在我的应用程序中创建一个处理Facebook登录的类. 我的问题是,它不适用于所有活动. 在该活动上,它不会调用回调. 关闭登录和授权应用程序Webview后,不会触发回调. Logcat中打印的最后一个状态是 OPENING

I am trying to make a class that handles facebook login in my application. My problem is it doesnt work on all activity. On that activity it doesnt call the callback. After the login and authorize application webview is dismiss, the callback doesnt fire. the last state that printed in Logcat is OPENING

这是我的代码:

public void doLogin() {


        if ((Session.getActiveSession() == null || !Session.getActiveSession().isOpened())) {
            List<String> permissions = new ArrayList<String>();
            permissions.add("email");

            // start Facebook Login
            openActiveSession(activity, true, new Session.StatusCallback() {

                // callback when session changes state
                @Override
                public void call(Session session, SessionState state,
                        Exception exception) {

                    Log.d("Sessionstate", state.toString());
                    if (session.isOpened()) {
                        // make request to the /me API
                        Request.executeMeRequestAsync(session,
                                new Request.GraphUserCallback() {

                                    @Override
                                    public void onCompleted(GraphUser user,
                                            Response response) {
                                        if (prgCheckFB.isShowing())
                                            prgCheckFB.dismiss();
                                        if (user != null) {

                                            Log.e("facebookid", id);
                                            doSomething(user);



                                        }
                                    }
                                });

                    } 
                }, permissions); 
        }
    }



private static Session openActiveSession(Activity activity,
        boolean allowLoginUI, Session.StatusCallback callback,
        List<String> permissions) {
    Session.OpenRequest openRequest = new Session.OpenRequest(activity)
            .setPermissions(permissions).setCallback(callback);
    Session session = new Session.Builder(activity).build();
    if (SessionState.CREATED_TOKEN_LOADED.equals(session.getState())
            || allowLoginUI) {
        Session.setActiveSession(session);
        session.openForRead(openRequest);
        return session;
    }
    return null;
}

doSomething是将用户数据保存为共享首选项的功能.

doSomething is function that will save the user data in a shared preference.

有什么问题吗?该功能在某些活动中有效,但在所有活动中无效.

Is there anything wrong? The function works in some activity but not ALL activities.

谢谢

推荐答案

也许您忘了登录后处理结果...

maybe you forget to handle the results after login...

检查替代方法 onActivityResult ..

因为它会将结果处理回MainActivity

because it handle the results back to the MainActivity,

也许这可以帮助您解决问题.

maybe this can help your problem..

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Session.getActiveSession().onActivityResult(this, requestCode,
            resultCode, data);
}

更新1

对于较新的SDK,请使用:

For newer SDK use:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    callbackManager.onActivityResult(requestCode,
            resultCode, data);
}

这篇关于Android Facebook SDK在登录后不会调用回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆