Android facebook sdk 登录后不调用回调
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20371842/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android facebook sdk does not call callback after login
提问by skaciw
Help,
帮助,
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
我正在尝试在我的应用程序中创建一个处理 facebook 登录的类。我的问题是它不适用于所有活动。在该活动中,它不会调用回调。登录和授权应用程序 webview 关闭后,回调不会触发。Logcat 中打印的最后一个状态是OPENING
Here is my code:
这是我的代码:
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 is function that will save the user data in a shared preference.
doSomething 是将用户数据保存在共享首选项中的函数。
Is there anything wrong? The function works in some activity but not ALL activities.
有什么不对的吗?该功能适用于某些活动,但不适用于所有活动。
Thank you
谢谢
回答by sedayux
maybe you forget to handle the results after login...
也许你登录后忘记处理结果......
check the override method onActivityResult..
检查覆盖方法onActivityResult..
because it handle the results back to the MainActivity,
因为它将结果处理回 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);
}
Update 1
更新 1
For newer SDK use:
对于较新的 SDK,请使用:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode,
resultCode, data);
}
回答by Unknown
Having the attribute android:noHistory="true"
set for your callback activity in the AndroidManifest.xml file will also result with the callback methods not firing.
在android:noHistory="true"
AndroidManifest.xml 文件中为您的回调活动设置属性也会导致回调方法不会触发。
回答by Gogo-the-Cat
I faced and solve this issue in a strange way. First it was working but after i added other social login buttons like Google login, i made its initialize between;
我以一种奇怪的方式面对并解决了这个问题。首先它可以工作,但是在我添加了其他社交登录按钮(例如 Google 登录)之后,我在两者之间进行了初始化;
CallbackManager.Factory.create();
CallbackManager.Factory.create();
and
和
LoginButton loginButton =(LoginButton) findViewById(R.id.button_facebook);
LoginButton loginButton =(LoginButton) findViewById(R.id.button_facebook);
After i moved google initialization to other lines, it has been solved.
在我将 google 初始化移动到其他行后,它已经解决了。
So in short words, facebook login initialization must be in a straight way without other code execution.
所以简而言之,facebook登录初始化必须是直接的,不需要其他代码执行。