Facebook SDK - 登录后没有任何反应

时间:2015-11-02 11:01:11

标签: java android

目前创建了一个Facebook登录按钮,我现在正尝试重定向到成功登录的新活动。为此,我当然使用onSucces()函数。

然而,当我点击我的应用程序上的登录时,它会加载,但没有任何反应。它保留在登录页面。

我的LoginActivity代码:

package com.example.frederik.testpleasework;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

import com.facebook.AccessToken;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.FacebookSdk;
import com.facebook.Profile;
import com.facebook.login.LoginResult;
import com.facebook.login.widget.LoginButton;

public class LoginActivity extends Activity {

    LoginButton login;
    TextView info;
    CallbackManager callbackManager;

    //Creating and initializing the feature(Code from Facebook install guide)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FacebookSdk.sdkInitialize(getApplicationContext()); //Calls for the facebook feature
        callbackManager = CallbackManager.Factory.create(); //Initializing callback, checking login result
        setContentView(R.layout.content_main);
        info = (TextView) findViewById(R.id.info);
        login = (LoginButton) findViewById(R.id.login_button);

        login.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                Intent i = new Intent(LoginActivity.this, LandingActivity.class);
                startActivity(i);
            }

            @Override
            public void onCancel() {

            }

            @Override
            public void onError(FacebookException e) {

            }
        });
    }

}

我下一个活动的代码(LandingActivity):

package com.example.frederik.testpleasework;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;

public class LandingActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.content_landing);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

}

我认为错误是在其中一个类中的某个地方,但如果您需要来自其他代码部分的更多信息,请随时请求。

编辑,androidmanifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.frederik.testpleasework" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar" >
        </activity>
        <activity
            android:name="com.facebook.FacebookActivity"
            android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.Translucent.NoTitleBar" />
        <activity android:name=".LoginActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <meta-data
            android:name="com.facebook.sdk.ApplicationId"
            android:value="@string/fbid" />

        <uses-permission android:name="android.permission.CAMERA" />
 //Requests permission to use the camera
        <uses-feature android:name="android.hardware.camera" />
 //A device NEEDS to have a camera to install the app
        <activity
            android:name=".LandingActivity"
            android:label="@string/title_activity_landing"
            android:theme="@style/AppTheme.NoActionBar" >
        </activity>
    </application>

</manifest>

3 个答案:

答案 0 :(得分:1)

来自官方的Facebook文档。 使用方法doFBLogin进行登录获取基本资料

 // Write only required permissions
    private String[] PERMISSION = new String[]{"user_photos", "email",
            "user_about_me", "public_profile", "user_about_me", "user_friends",
            "user_likes", "user_hometown",
            "user_education_history", "user_work_history", "user_birthday"};  

    private CallbackManager callbackmanager;
    private String strFirstname;
    private String strLastname;
    private String strEmail;
    private String strProfilePhoto;
    private String strGender;

  private void doFBLogin() {
            callbackmanager = CallbackManager.Factory.create();
            // Set permissions
            LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList(PERMISSION));
            LoginManager.getInstance().registerCallback(callbackmanager, new FacebookCallback<LoginResult>() {
                @Override
                public void onSuccess(LoginResult loginResult) {
                    GraphRequest request = GraphRequest.newMeRequest(
                            loginResult.getAccessToken(),
                            new GraphRequest.GraphJSONObjectCallback() {
                                @Override
                                public void onCompleted(
                                        JSONObject json,
                                        GraphResponse response) {
                                    if (response.getError() != null) {
                                        new ShowToast(SplashScreen.this, getResources().getString(R.string.failed_login));
                                    } else {
                                        try {
                                            strFirstname = json.getString("first_name") + " " + json.getString("last_name");
                                            strLastname = json.getString("last_name");
                                            strEmail = json.getString("email");
                                            strProfilePhoto = json.getJSONObject("picture").getJSONObject("data").getString("url");
                                            strGender = json.getString("gender");

                                        } catch (JSONException e) {
                                        }
                                    }

                                }
                            });
                    Bundle parameters = new Bundle();
                    parameters.putString("fields", "id,name,email,gender,birthday,first_name,last_name,picture.type(large)");
                    request.setParameters(parameters);
                    request.executeAsync();
                }

                @Override
                public void onCancel() {
                }

                @Override
                public void onError(FacebookException error) {
                    new ShowToast(SplashScreen.this, getResources().getString(R.string.failed_login));
                }
            });


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

manifest执行以下操作。

    <!-- For detect network connections -->
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

                <activity
                    android:name="com.facebook.FacebookActivity"
                    android:label="@string/app_name"
                    android:theme="@android:style/Theme.Translucent.NoTitleBar" />

                <meta-data
                    android:name="com.facebook.sdk.ApplicationId"
                    android:value="@string/app_id" />
                <!-- Provider -->
                <provider
                    android:name="com.facebook.FacebookContentProvider"
                    android:authorities="com.facebook.app.FacebookContentProvider1460656834264602"
                    android:exported="true"
                    android:writePermission="true" />

答案 1 :(得分:1)

第一步,init facebook sdk:

FacebookSdk.sdkInitialize(this.getApplicationContext());

第二步,创建回叫管理器:

CallbackManager callbackManager = CallbackManager.Factory.create();

第三步,添加读取权限:

LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile", "user_friends"));

下一步,注册回拨:

LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {...});

您可以将上面的所有代码放到onCreate方法中。最后,你必须为回调添加onActivityResult

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

试试这段代码,希望对您有所帮助。您可以参考this

答案 2 :(得分:0)

  mButtonLogin.registerCallback(callbackManager,
            new FacebookCallback<LoginResult>() {
                @Override
                public void onSuccess(LoginResult loginResult) {
                    handlePendingAction();
                    GraphRequest request = GraphRequest.newMeRequest(
                            loginResult.getAccessToken(),new GraphRequest.GraphJSONObjectCallback() {


                                @Override
                                public void onCompleted(JSONObject object,GraphResponse response) {
                                    Log.v("LoginActivity", response.toString());  
                                    try {
                                         Intent i = new Intent(LoginActivity.this, LandingActivity.class);
                                        startActivity(i);

                                    } catch (JSONException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                    }
                                }                                   
                            });
                    request.executeAsync();
                }

                @Override
                public void onCancel() {

                }

                @Override
                public void onError(FacebookException exception) {

                }


            });

尝试在登录活动中添加此代码。