Android接入Facebook的分享
首先注册成为Fackbook开发者。 https://developers.facebook.com
登陆到Facebook开发者后台然后点击"Apps" -> "Add a New App",输入App的名称“TeshShare”并选择分类,进行创建App
点击 "Apps" -> "TestShare" ,就是我们刚才创建的App ,然后点击左侧的"Settings",进行相关的信息设置.
配置如下信息
(1). App的包名 com.test.share
(2). App的主Activity名字 com.test.share.MainActivity
(3). 签名文件的哈希值
eclipse debug签名hash值获取方法 打开mac终端输入下面的命令
keytool -exportcert -alias androiddebugkey -keystore ~/.android/debug.keystore | openssl sha1 -binary | openssl base64
正式签名需要在程序中添加如下代码签名打包后查看控制台的输入值将这个值复制到后台进行配置。这个我们先使用Eclipse的debug签名哈希值。代码如下
/** * 获取签名Key的hash值facebook后台需要添加这个如果这个值跟后台的对不上那么会分享失败提示Key Hash值不对 */ public static void getKeyHash(Context ctx){ // Add code to print out the key hash try { PackageInfo info = ctx.getPackageManager().getPackageInfo( "com.test.share", //替换成你app的包名 PackageManager.GET_SIGNATURES); for (Signature signature : info.signatures) { MessageDigest md = MessageDigest.getInstance("SHA"); md.update(signature.toByteArray()); Log.d("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT)); } } catch (Exception e) { } }
4.下载 Facebook SDK 如下图。讲facebook导入到Eclipse中。将导入的facebook做为lib工程add到我们的工程中
乖乖的打开×××,到Google Play上去安装Facebook吧。
5.配置facebook appid和网络访问权限
在strings.xml中添加
<string name ="facebook_app_id">365646333616560</string>
在AndroidManifest.xml添加
<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/>
<uses-permission android:name="android.permission.INTERNET" />
6.列出MainActivity的代码
package com.test.share;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Toast;
import com.facebook.UiLifecycleHelper;
import com.facebook.widget.FacebookDialog;
public class MainActivity extends Activity {
private UiLifecycleHelper uiHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
uiHelper = new UiLifecycleHelper(this, null);
uiHelper.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Activity activity = this;
findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// 判断是否安装Facebook
if (FacebookDialog.canPresentShareDialog(activity,
FacebookDialog.ShareDialogFeature.SHARE_DIALOG)) {
FacebookDialog shareDialog = new FacebookDialog.ShareDialogBuilder(
activity).setLink("www.baidu.com")
.setApplicationName("TestShare").setName("test")
.setCaption("this is a test share ").build();
uiHelper.trackPendingDialogCall(shareDialog.present());
} else {
Toast.makeText(activity, "Facebook app is not installed",
Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
uiHelper.onActivityResult(requestCode, resultCode, data, new FacebookDialog.Callback() {
@Override
public void onError(FacebookDialog.PendingCall pendingCall, Exception error, Bundle data) {
Log.e("Activity", String.format("Error: %s", error.toString()));
}
@Override
public void onComplete(FacebookDialog.PendingCall pendingCall, Bundle data) {
Log.i("Activity", "Success!");
}
});
}
@Override
protected void onResume() {
super.onResume();
uiHelper.onResume();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
uiHelper.onSaveInstanceState(outState);
}
@Override
public void onPause() {
super.onPause();
uiHelper.onPause();
}
@Override
public void onDestroy() {
super.onDestroy();
uiHelper.onDestroy();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
7.运行测试
点击发布提示“分享成功”
8.注意的问题
(1). 分享时候的AppName问题
//setApplicationName("必须是你后台设置的AppName")
FacebookDialog shareDialog = new FacebookDialog.ShareDialogBuilder(
activity).setLink("www.baidu.com")
.setApplicationName("TestShare").setName("test")
.setCaption("this is a test share ").build();
(2). 发布的时候签名哈希值在后台要改为正式签名的哈希值
(3). 关于混淆
-keep class com.facebook.** { *; }
-keepattributes Signature
(4).正式发布的时候后台要改一个状态
"Status&Review" -> "Do you want to meak this app and all its live...public ? " -> "YES"
最后附上Demo×××地址
好了,今天跟大家分享到这里。
转载于:https://blog.51cto.com/vincenttung/1587351