简体   繁体   English

Android Facebook登录未调用回调函数

[英]Android Facebook login not calling callback function

I am trying to add a Facebook login button in the main activity of a simple application. 我试图在一个简单应用程序的主要活动中添加一个Facebook登录按钮。 When I press the "login with Facebook" button, I have the FB application open for login, but after the successful login, the app comes back to the main screen of my application and nothing happens (the callback function registered for the LoginManager is not called). 当我按下“使用Facebook登录”按钮时,我打开了FB应用程序进行登录,但是成功登录后,该应用程序将返回到应用程序的主屏幕,并且没有任何反应(没有为LoginManager注册的回调函数称为)。

My code looks like: 我的代码如下:

public class MainActivity extends FragmentActivity {

    Button changeButton;
    LoginButton loginButton;
    CallbackManager callbackManager;
    MainActivity thisActivity;

    // Sets the Facebook login.
    private void setupFacebookLogin() {

        // Create the callbackManager and add functionality to it.
        callbackManager = CallbackManager.Factory.create();
        LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                throw new AssertionError();
            }

            @Override
            public void onCancel() {

            }

            @Override
            public void onError(FacebookException e) {

            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        thisActivity = this;

        // Initialize Facebook SDK.
        FacebookSdk.sdkInitialize(getApplicationContext());

        setupFacebookLogin();

        // Set the current layout to main.
        setContentView(R.layout.activity_main);
    }

    @Nullable
    @Override
    public View onCreateView(String name, Context context, AttributeSet attrs) {
        return super.onCreateView(name, context, attrs);
    }

    @Override
    protected void onResume() {
        super.onResume();
        AppEventsLogger.activateApp(this);
    }
    @Override
    public void onPause() {
        super.onPause();
        AppEventsLogger.deactivateApp(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

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

Also, my xml is: 另外,我的xml是:

<TextView android:text="@string/hello_world" android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textView" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="change"
    android:id="@+id/button1"
    android:layout_below="@+id/textView"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="61dp" />

<com.facebook.login.widget.LoginButton
    android:id="@+id/fb_login_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_below="@+id/button1"
    android:layout_centerHorizontal="true" />

I would expect the application to crash when it returns from FB login, due to the assertion in onSuccess. 由于onSuccess中的断言,我希望应用程序从FB登录返回时崩溃。 However, it is not the case. 但是,事实并非如此。 Any help would be appreciated, thank you! 任何帮助,将不胜感激,谢谢!

在“使用Facebook登录”按钮的点击监听器中添加以下行:

LoginManager.getInstance().logInWithReadPermissions(WelcomeActivity1.this, (Arrays.asList("public_profile", "user_friends","user_birthday","user_about_me","email")));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM