官方說明文件:https://developers.facebook.com/docs/getting-started/facebook-sdk-for-android/3.0/
1.Install the Prerequisites
You need Eclipse, the Android SDK, and the ADT Plugin
就是安裝Eclipse起來,可以使用Android Project就可以。2.Install the Facebook SDK for Android and the Facebook APK
Download and extract the installation package. Install the Facebook app to your emulator
下載Facebook SDK並安裝Facebook.apk3.Import the SDK into Eclipse
Add the SDK as a library project to your workspace, along with the sample projects
就是import facebook sdk(其他附屬的sample可以不用import 無所謂)這邊要注意的就是java compiler版本
4.Run the Samples
Try the sample projects, and add your Android key hash to your Facebook profile
第一個Run的Sample是這個- ProfilePictureSample: demonstrates non-logged-in usage of the profile picture control
這邊的重點是Library的Add..因為位置不同,記得要重新add
再來就是import android.support.v4的問題...記得要Add External JARs,這個檔案在Facebook SDK/libs裡面,有些人會copy到自己的project的libs裡面,應該都是可以的。
先確定第一個sample可以Run 就可以進行下一個步驟了
第二個要測試的AP是
- HelloFacebookSample: an all-round sample that demonstrates profile access, status updates and photo uploading
keytool -exportcert -alias androiddebugkey -keystore ~/.android/debug.keystore | openssl sha1 -binary | openssl base64
切忌不要輸入密碼,直接按Enter既可,會得到一組碼,然後再oncreate中加入下面的code:@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Add code to print out the key hash
try {
PackageInfo info = getPackageManager().getPackageInfo(
"com.facebook.samples.hellofacebook",
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 (NameNotFoundException e) {
} catch (NoSuchAlgorithmException e) {
}
執行確認一下android hash key 是否正確。5.Create a Facebook App
Create a new Facebook app, get its ID and associate your Android key hash with it
這邊重點注意,在Facebook developers中設定New APP的時候Sandbox(沙盒模式)記得要關閉,這樣這個APP才會變成"This app is live",這個沒轉換,也是會無法連結,在if (session.isOpened())
這邊會ㄧ直為false,因為無法登入。
剩下步驟就沒有什麼問題,請參考官方網站說明

