Android Facebook分享功能实现

1、下载 Facebook SDK

https://developers.facebook.com/docs/Android?locale=zh_CN

2、在facebook下设置app的相关信息

3、主要代码

 

private CallbackManager callbackManager;
private ShareDialog shareDialog;
private boolean canPresentShareDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FacebookSdk.sdkInitialize(this.getApplicationContext());
    callbackManager = CallbackManager.Factory.create();
    shareDialog = new ShareDialog(this);
    shareDialog.registerCallback(callbackManager,shareCallback);
setContentView(R.layout.share);
   
    ImageView bt_list_share = (ImageView) findViewById(R.id.bt_list_share);//facebook分享有自带的sharebutton  这里就自定义了一张图片了

    bt_list_share.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
	ShareLinkContent linkContent = new ShareLinkContent.Builder()
        .setContentUrl(Uri.parse("http://www.baidu.com"))
        .setContentTitle("facebook share")
        .setContentDescription("good description")
        .build();
	shareDialog.show(linkContent);
} });}

设置回调:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    callbackManager.onActivityResult(requestCode, resultCode, data);
}
private FacebookCallback<Sharer.Result> shareCallback = new FacebookCallback<Sharer.Result>() {
    @Override
    public void onCancel() {
        Log.d("Facebook", "Canceled");
    }

    @Override
    public void onError(FacebookException error) {
        Log.d("Facebook", String.format("Error: %s",error.toString()));
    }

    @Override
    public void onSuccess(Sharer.Result result) {
        Log.d("HelloFacebook", "Success!");
    }
};

最后别忘了AndroidManifest.xml中配置下

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

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

<provider android:authorities="com.facebook.app.FacebookContentProvider[app_id]"
          android:name="com.facebook.FacebookContentProvider"
          android:exported="true"/>

 

好了 主要的基本就这么多了  祝大家好运能顺利实现,有不足的地方还望指正~~~

posted on 2016-09-23 14:43  Sun‘刺眼的博客  阅读(5718)  评论(0编辑  收藏  举报

导航