1. 將facebook專案加入lib
2. 設定manifest.xml
<uses-permission android:name="android.permission.INTERNET"/> <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/app_id"/> <activity android:name="com.facebook.LoginActivity"/>
3. 取得key hashes
C:\Program Files\Java\jdk1.6.0_37\bin>keytool -exportcert -alias androiddebugkey -keystore ~/.android/debug.keystore | " C:\openssl-0.9.8k_X64\bin\openssl.exe" sha1 -binary | "C:\openssl-0.9.8k_X64\bin\openssl.exe" base64
4. button的onClick()實作login及發佈的功能
Session.openActiveSession(MainActivity.this, true, new Session.StatusCallback() { @Override public void call(Session session, SessionState state,Exception exception) { if (session.isOpened()) { Log.d("fbt",session.getAccessToken()); // get token publishFeedDialog(); } } });
private void publishFeedDialog() { Bundle params = new Bundle(); params.putString("name", "Facebook SDK for Android"); params.putString("caption", "Build great social apps and get more installs."); params.putString("description", "The Facebook SDK for Android makes it easier and faster to develop Facebook integrated Android apps."); params.putString("link", "https://developers.facebook.com/android"); params.putString("picture", "https://raw.github.com/fbsamples/ios-3.x-howtos/master/Images/iossdk_logo.png"); WebDialog feedDialog = ( new WebDialog.FeedDialogBuilder(MainActivity.this, Session.getActiveSession(), params)) .setOnCompleteListener(new WebDialog.OnCompleteListener() { @Override public void onComplete(Bundle values, FacebookException error) { if (error == null) { // When the story is posted, echo the success // and the post Id. final String postId = values.getString("post_id"); if (postId != null) { Toast.makeText(MainActivity.this, "Posted story, id: "+postId, Toast.LENGTH_SHORT).show(); } else { // User clicked the Cancel button Toast.makeText(MainActivity.this, "Publish cancelled", Toast.LENGTH_SHORT).show(); } } else if (error instanceof FacebookOperationCanceledException) { // User clicked the "x" button Toast.makeText(MainActivity.this, "Publish cancelled", Toast.LENGTH_SHORT).show(); } else { // Generic, ex: network error Toast.makeText(MainActivity.this, "Error posting story", Toast.LENGTH_SHORT).show(); } } }) .build(); feedDialog.show(); }
沒有留言:
張貼留言