Facebook官方sdk对接

facebook原生的登陆和分享及超链接跳转功能:

  • 1.登陆 developer.facebook.com,创建app,输入包名和key的SSH值(SSH在windows系统上通过openssl的终端命令获取,如SourceTree就自带ssh的终端命令,mac更方便),会自动搜索google play上的程序
  • 2.配置基础信息,在AndroidManifest.xml中:包含
        <meta-data
            android:name="com.facebook.sdk.ApplicationId"
            android:value="@string/facebook_app_id" />
        <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" />
        <provider android:name="com.facebook.FacebookContentProvider"
                android:authorities="com.facebook.app.FacebookContentProvider123456"
                 android:exported="true" />    

注意: 其中的123456指的是facebook分配给你的app的一个App ID,在Dashboard界面就能看到,替换即可。

gradle中配置:

{
    compile 'com.facebook.android:facebook-android-sdk:4.7.0'
}
  • 3.登陆功能:
private CallbackManager callbackManager;
@Override
protected void onCreate(...){
    LoginManager.getInstance().registerCallback(callbackManager,
                    new FacebookCallback() {
                        @Override
                        public void onSuccess(LoginResult loginResult) {
                            // App code
                            AccessToken token = loginResult.getAccessToken();
                            String openId = token.getUserId();
                        }

                        @Override
                        public void onCancel() {
                            // App code
                        }

                        @Override
                        public void onError(FacebookException exception) {
                            // App code
                        }
                    });
        }
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    callbackManager.onActivityResult(requestCode,resultCode,data);
}
public void login(View view){
    LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile", "user_friends"));
    }
  • 4.分享功能:
private CallbackManager mCallBackManager;
private ShareDialog mShareDialog;
@Override
protected void onCreate(...){
    mCallBackManager = CallbackManager.Factory.create();
    mShareDialog = new ShareDialog(this);
}
public void share(View view){
    if (ShareDialog.canShow(ShareLinkContent.class)) {
        ShareLinkContent linkContent = new ShareLinkContent.Builder()
                   .setContentTitle(shareInfo.getTitle())
                   .setContentDescription(shareInfo.getContent())
                   .setContentUrl(Uri.parse(shareInfo.getLink()))
                   .setImageUrl(Uri.parse(shareInfo.getIcon()))
                   .build();
           mShareDialog.show(this, linkContent);
     }
}

- 5.applink功能(DeepLink)
新建activity,AndroidManifest.xml配置

<activity name=".DeepLinkActivity">
     <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:scheme="myhttp"
                    android:host="mycompany"/>
            intent-filter>
activity>

在onCreate方法中使用SDK提供的AppLink.getTargetUrl()获取协议解析条转即可

你可能感兴趣的:(android,studio)