分类 FaceBook SDK 下的文章

FaceBook SDK登录功能实现(Eclipse)


由于公司游戏要进行海外推广,所以要我们接入FBSDK 实现登录,分享,投放,所以写这篇文章,也算是个工作总结。
1、资料

(1).FB SDK github源码地址为
(2): [FB SDK中文接入文档]
2.创建应用,获取应用编号和密钥
这些事情,文档中都有详细描述,我就不进行赘述了
3.下载FB SDK,并配置参数
(1).解压facebook-android-sdk-4.25.0.aar文件,并将其中的assets,res,AndroidManifest.xml,classes.jar等复制的eclipse 新建工程下,并配置项目为library

(2).由于FB SDK引用了v7包和资源,所以要把v7项目引入到FB SDk中来(注:不只是v7包,项目一般在你的sdk目录下的extrasandroidsupportv7)

(3) 配置参数,在eclipse res/values/strings.xml中加入

<string name="facebook_app_id">133094273807591</string>

(4),在AndroidManifest中,添加权限,配置参数

<uses-permission android:name="android.permission.INTERNET"/>

application下配置参数

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

将FB SDK自定义Activity加到application中

<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" /> 

(5).引入新建的FB sdk工程

4.接入 FB SDK

(1).在自定义application 中初始化SDK参数

public class MyApplication extends Application{

    public void onCreate() {
        super.onCreate();
        FacebookSdk.sdkInitialize(getApplicationContext());
        AppEventsLogger.activateApp(this);
    }
}

(2),新建LoginActivity接入FB SDK代码

public class LoginActivity extends Activity {
    private static final String PERMISSION = "public_profile";

    private static final int PICK_PERMS_REQUEST = 0;

    private CallbackManager callbackManager;

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
     // 注册登录回调
        callbackManager = CallbackManager.Factory.create();
        LoginManager loginManager = LoginManager.getInstance();
        loginManager.setDefaultAudience(loginManager.getDefaultAudience());
        loginManager.setLoginBehavior(loginManager.getLoginBehavior());
        loginManager.logInWithReadPermissions(LoginActivity.this,
                Arrays.asList(PERMISSION));
        LoginManager.getInstance().registerCallback(callbackManager,
                new FacebookCallback<LoginResult>() {
                    @Override
                    public void onSuccess(final LoginResult loginResult) {
                        // App code
                        Toast.makeText(LoginActivity.this, "登录成功",
                                Toast.LENGTH_LONG).show();
                        Intent intent = new Intent(LoginActivity.this,
                                AppActivity.class);
                        startActivity(intent);
                    }

                    @Override
                    public void onCancel() {
                        // App code
                        Toast.makeText(LoginActivity.this, "登录失败,请稍后重试",
                                Toast.LENGTH_LONG).show();
                    }

                    @Override
                    public void onError(final FacebookException exception) {
                        // App code
                        Toast.makeText(LoginActivity.this, "登录失败,请稍后重试",
                                Toast.LENGTH_LONG).show();
                    }
                });
      

    }

    /**
    * 处理登录结果
    */

    @Override
    protected void onActivityResult(final int requestCode,
            final int resultCode, final Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == PICK_PERMS_REQUEST) {
            if (resultCode == RESULT_OK) {
                LoginManager loginManager = LoginManager.getInstance();
                loginManager.setDefaultAudience(loginManager
                        .getDefaultAudience());
                loginManager.setLoginBehavior(loginManager.getLoginBehavior());
                loginManager.logInWithReadPermissions(LoginActivity.this,
                        Arrays.asList(PERMISSION));
            }
        } else {
            callbackManager.onActivityResult(requestCode, resultCode, data);
        }
    }
}