在分享之前,你需要做以下几件事情:
dependencies {
compile 'com.facebook.android:facebook-android-sdk:4.7.0'
}
注意:Facebook支持的最小SDK版本为15,所以有两种方法:第一、将minSdkVersion设置为15;第二、在AndroidManifest.xml中添加
,然后在代码中判断版本进行选择。
1.在strings.xml 中添加一个name为facebook_app_id并且value为你的app id的字符串;
注意:必须用string引入到AndroidManifest.xml,否则会报错
2.在AndroidManifest.xml添加联网权限:
<uses-permission android:name="android.permission.INTERNET"/>
3.添加mete-date:
"@string/app_name" ...>
...
"com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/>
...
sudo apt-get install openssl
获取方法可以参照他的博客:http://blog.csdn.net/h7870181/article/details/8572402
- 添加Facebook Activity 并在AndroidManifest.xml中声明
注意:这个activity的声明必须添加,同时这个Activity非自己创建的,而是SDK自带的
"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.FacebookContentProvider1234"
android:name="com.facebook.FacebookContentProvider"
android:exported="true" />
现在我们开始针对Share Dialog 来进行分享,使用Share Dialog主要是因为可以获得分享的回调结果,对此我们可以做相应的处理。同时如果未安装Facebook,它会自动退回。
public class MainActivity extends Activity {
CallbackManager callbackManager;
ShareDialog shareDialog;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ShareBean bean= (ShareBean) getIntent().getSerializableExtra("bean");
FacebookSdk.sdkInitialize(getApplicationContext());
LoginManager.getInstance().logInWithPublishPermissions(this, Arrays.asList("publish_actions")); //这一句很重要,如果不加,你会发现,虽然可以分享成功,但是为神马返回的都是true,即使你跳转过去并没有分享,而这一句主要是针对应用赋权
callbackManager = CallbackManager.Factory.create();
shareDialog = new ShareDialog(this);
shareDialog.registerCallback(callbackManager, new FacebookCallback() {
ShareFBProvider shareFBProvider = ShareFBProvider.getInstance()
public void onSuccess(Sharer.Result result) {
shareFBProvider.handleShareResult(shareFBProvider.SHARE_OK);
finish(); //得到回调结果后必须finish,因为为无界面的Acticity,会导致下面的Activity处于pause状态而无法操作
}
@Override
public void onCancel() {
shareFBProvider.handleShareResult(shareFBProvider.SHARE_CANCEL);
finish();
}
@Override
public void onError(FacebookException error) {
shareFBProvider.handleShareResult(-1);
finish();
}
});
if (ShareDialog.canShow(ShareLinkContent.class)) {
ShareLinkContent linkContent = new ShareLinkContent.Builder()
.setContentTitle(bean.title)
.setContentDescription(bean.description)
.setContentUrl(Uri.parse(bean.url))
.build();
shareDialog.show(linkContent);
}else {
Toast.makeText(this,"未安装facebook", Toast.LENGTH_LONG);
}
}
@Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
}
上面是一个没有界面的仅仅用于分享的Activity,只要跳转到这个Activity就会分享,然后分享成功亦或者不成功最终都会回到这个Activity。OK,基本的我们说完了,下面我们为了代码的可重用性,做如下优化:
1、建一个ShareBean类,这是分享时可能需要传递的数据:
public class ShareBean implements Serializable {
public String url = "";
public String title = "";
public String description = "";
}
2、建立一个ShareFBProvider类,这个类用于过渡,上面的分享Activity属于通用,但是回调所要做的处理可能不一样,因此,在上面的回调中是通过调用这个类中的handleShareResult方法来触发假回调,真正需要做操作的方法中实现里面的IFBShareCallback接口并且调用shareTo方法:
public class ShareFBProvider{
private IFBShareCallback mCallBack;
private static ShareFBProvider mProvider;
public static final int SHARE_OK = 0;
public static final int SHARE_CANCEL = 1;
public static getInstance() {
if (mProvider== null) {
mProvider= new ShareFBProvider();
}
return mProvider;
}
public void shareTo(Context context, SharedBean sharedBean) {
Intent intent = new Intent(context, MainActivity.class);
Bundle bundle = new Bundle();
bundle.putSerializable("bean",sharedBean);
intent.putExtras(bundle);
context.startActivity(intent);
}
public void registerCallback(IFBShareCallback callBack) {
mCallBack= callBack;
}
public void unregisterCallback() {
mCallBack= null;
}
public void handleShareResult(int result) {
switch(result) {
case SHARE_OK :
if (null != mCallBack) {
mCallBack.shareFBSuccess();
}
break;
case SHARE_CANCEL :
if (null != mCallBack) {
mCallBack.shareFBCancel();
}
break;
default:
if (null != mCallBack) {
mCallBack.shareFBFail();
}
break;
}
}
public interface IFBShareCallback{
void shareFBSuccess();
void shareFBCancel();
void shareFBFail();
}
建立一个Util工具类,任何地方调用share方法即可以分享,分享平台的选择处理也在这个方法中:
public class Utils {
public static void share(Context context, IShareCallback shareCallback) {
final ShareBean shareBean = new shareBean();
shareBean.url = "http://www.baidu.com";
shareBean.title = "百度";
...
//主要判断哪个应用安装、判断语言等等,然后选择分享到哪个平台
...
//如果分享到FaceBook,必须判断App版本是否大于15
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
//针对低版本的分享
...
} else {
shareToFB(shareBean , context, shareCallback);
}
}
private static void shareToFB(final ShareBean bean, Context context, final IShareCallback shareCallback) {
final ShareFBProvider provider = ShareFBProvider.getInstance();
//分享跳转
provider.shareTo(context, info);
//注册回调
provider.registerCallback(new ShareFBProvider.IFBShareCallback() {
@Override
public void shareFBSuccess() {
provider.unregisterCallback();
if (null != shareCallback) {
shareCallback.onShareSuccess(IShareAppCallback.SOURCE_FACEBOOK);
}
}
@Override
public void shareFBFail() {
provider.unregisterCallback();
if (null != shareCallback) {
shareCallback.onShareFail();
}
}
@Override
public void shareFBCancel() {
provider.unregisterCallback();
if (null != shareCallback) {
shareCallback.onShareCancel();
}
}
});
}
public interface IShareCallback {
String SOURCE_WEIXIN = "Weixin";
String SOURCE_FACEBOOK = "Facebook";
void onShareSuccess(String source);
void onShareFail();
void onShareCancel();
}
}
最后,在任何一个地方调用Utils.share(context, shareCallback);
就可以分享咯。
下面附上Facebook官方分享链接:https://developers.facebook.com/docs/sharing/android