Android Facebook登录中不会调用回调函数

我正在开发一款Android应用.在我的应用程序中,我正在整合Facebook登录.我之前曾经开发过一次Facebook登录.当我开发这个时候,不会调用Facebook回调函数.我也无法检查错误.我不知道出了什么问题.

我使用Gradle安装了Facebook SDK

compile 'com.facebook.android:facebook-android-sdk:4.1.0'

然后我生成了密钥哈希并在Facebook开发人员设置中设置如下:

enter image description here

这是我的表现

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="tonightfootballreport.com.tfr" >
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <application
        android:name="tonightfootballreport.model.TfrApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >
        <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/>
        <activity
            android:configChanges="orientation|screenSize"
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:configChanges="orientation|screenSize"
            android:name=".FacebookConfigActivity"
            android:label="Facebook"
            android:theme="@style/AppTheme.NoActionBar" >
            <intent-filter>
                <action android:name="tonightfootballreport.com.tfr.FacebookConfigActivity"/>
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity android:name="com.facebook.FacebookActivity"
            android:configChanges=
                "keyboard|keyboardHidden|screenLayout|screenSize|orientation"
            android:theme="@android:style/Theme.Translucent.NoTitleBar"
            android:label="@string/app_name" />

        <provider android:authorities="com.facebook.app.FacebookContentProvider1817380365215100"
            android:name="com.facebook.FacebookContentProvider"
            android:exported="true" />
    </application>

</manifest>

正如您所看到的,Facebook登录不在主要活动中,因此我在Developer Settings中设置了FacebookConfigActivity.

然后,当我单击MainActivity中的按钮时,将打开FacebookConfigActivity并执行Facebook登录.以下是代码.

public class FacebookConfigActivity extends AppCompatActivity {
    private CallbackManager callbackManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FacebookSdk.sdkInitialize(getApplicationContext(), AppConfig.FACEBOOK_REQUEST_CODE);
        AppEventsLogger.activateApp(this);
        setContentView(R.layout.activity_facebook);
        setUpCallBack();
        loginInToFacebook();
    }


    private void loginInToFacebook()
    {
        LoginManager.getInstance().logInWithReadPermissions(FacebookConfigActivity.this,Arrays.asList("public_profile"));
    }

    private void setUpCallBack()
    {
        callbackManager = CallbackManager.Factory.create();

        LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                //show share alert
                //then share
                Log.i("LOGIN_SUCCESS","LOGIN_SUCCESS");
            }


            @Override
            public void onCancel() {
                Toast.makeText(getBaseContext(), "Operation canceled", Toast.LENGTH_SHORT).show();
                finish();
            }

            @Override
            public void onError(FacebookException error) {
                Toast.makeText(getBaseContext(), "Error in connecting to Facebook", Toast.LENGTH_SHORT).show();
                finish();
            }
        });
    }
}

如您所见,这只是简单的Facebook登录.因此,当我单击登录按钮时,我会在Facebook登录表单中输入凭据,您可以在下面看到.

enter image description here

然后在下一步单击“确定”后,将显示FacebookConfigActivity,并且不会调用任何回调函数.如果我单击取消,则不会调用Facebook取消回调.什么是可能的错误?

最佳答案
你忘了把监听器放在onActivityResult()中

将以下代码放在onActivityResult活动方法中:

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

转载注明原文:Android Facebook登录中不会调用回调函数 - 代码日志