您好,登錄后才能下訂單哦!
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×××地址
Demo
好了,今天跟大家分享到這里。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。