現在開発中のアプリにfacebook SDKを入れてみました。
このSDK頻繁にアップデートが行われているようで、ちょっと前の記事を参考にすると、コンパイルすら通らない状況です。
facebook SDKの導入については、こちらを参考にしました。
ただ、この通りに書いてもコンパイルが通らないので、こんな感じで書きました。
// ログインボタン
private Button mFbloginBtn;
// ライフサイクルヘルパー
private UiLifecycleHelper mUiHelper;
// セッションステートコールバックメソッド
private Session.StatusCallback mFacebookCallback = new Session.StatusCallback() {
@Override
public void call(Session session, SessionState state,
Exception exception) {
onSessionStateChange(session, state, exception);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_facebook_login);
// Set up the action bar to show a dropdown list.
final ActionBar actionBar = getActionBar();
actionBar.setDisplayShowTitleEnabled(false);
// actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
// UPアイコンを表示
getActionBar().setDisplayHomeAsUpEnabled(true);
mLoginFormView = findViewById(R.id.login_form);
mLoginStatusView = findViewById(R.id.login_status);
sign_in_button = (TextView) findViewById(R.id.sign_in_button);
// ライフサイクルヘルパーの初期化
mUiHelper = new UiLifecycleHelper(this, mFacebookCallback);
mUiHelper.onCreate(savedInstanceState);
// ログインボタンの初期化
mFbloginBtn = (Button) findViewById(R.id.sign_in_button);
mFbloginBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
if (bLogin) {
// ログアウト
logoutFacebook();
} else {
// ログイン
loginFacebook();
}
}
});
}
private void loginFacebook() {
// リクエストの生成
OpenRequest openRequest = new OpenRequest(this)
.setCallback(mFacebookCallback);
// emailを要求するパーミッションを設定
openRequest.setPermissions(Arrays.asList("email"));
// セッションを生成
// Session session = new Builder(this).build();
Session session = Session.getActiveSession();
// アクティブセッションとする。
Session.setActiveSession(session);
// 認証を要求する。
session.openForRead(openRequest);
return;
}
private void logoutFacebook() {
Session session = Session.getActiveSession();
if (!session.isClosed()) {
// セッションとトークン情報を廃棄する。
session.closeAndClearTokenInformation();
bLogin = false;
sign_in_button.setText(R.string.label_facebook_login);
}
}
private void onSessionStateChange(Session session, SessionState state,
Exception exception) {
if (session.isOpened()) {
// GraphAPIのmeリクエストを呼び出す。
Request.executeMeRequestAsync(session,
new FacebookGraphUserCallback("wait...") {
@Override
public void onCompleted(GraphUser user,
Response response) {
super.onCompleted(user, response);
// Log.d(TAG, "user = " +
// user.getInnerJSONObject());
Toast.makeText(cMe,
String.valueOf(user.getInnerJSONObject()),
Toast.LENGTH_LONG).show();
bLogin = true;
sign_in_button.setText(R.string.pref_logoff);
String userName = user.getUsername();
String userId = user.getId();
String userBirth = user.getBirthday();
// TextView txtUser = (TextView) findViewById(R.id.txtUser);
// txtUser.setText(userId + " " + userName + " " + userBirth);
}
});
}
}
@Override
public void onPause() {
super.onPause();
mUiHelper.onPause();
}
@Override
public void onDestroy() {
super.onDestroy();
mUiHelper.onDestroy();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
mUiHelper.onActivityResult(requestCode, resultCode, data);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mUiHelper.onSaveInstanceState(outState);
}
/**
* Shows the progress UI and hides the login form.
*/
@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
private void showProgress(final boolean show) {
// On Honeycomb MR2 we have the ViewPropertyAnimator APIs, which allow
// for very easy animations. If available, use these APIs to fade-in
// the progress spinner.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
int shortAnimTime = getResources().getInteger(
android.R.integer.config_shortAnimTime);
mLoginStatusView.setVisibility(View.VISIBLE);
mLoginStatusView.animate().setDuration(shortAnimTime)
.alpha(show ? 1 : 0)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
mLoginStatusView.setVisibility(show ? View.VISIBLE
: View.GONE);
}
});
mLoginFormView.setVisibility(View.VISIBLE);
mLoginFormView.animate().setDuration(shortAnimTime)
.alpha(show ? 0 : 1)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
mLoginFormView.setVisibility(show ? View.GONE
: View.VISIBLE);
}
});
} else {
// The ViewPropertyAnimator APIs are not available, so simply show
// and hide the relevant UI components.
mLoginStatusView.setVisibility(show ? View.VISIBLE : View.GONE);
mLoginFormView.setVisibility(show ? View.GONE : View.VISIBLE);
}
}
class FacebookGraphUserCallback implements Request.GraphUserCallback {
private ProgressDialog mProgress = null;
public FacebookGraphUserCallback(String message) {
mProgress = new ProgressDialog(FacebookLoginActivity.this);
mProgress.setMessage(message);
mProgress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
mProgress.show();
}
@Override
public void onCompleted(GraphUser user, Response response) {
mProgress.dismiss();
}
}
認証が完了すると、facebook上の一通りの情報が取得できます。
ユーザ名・ID・誕生日を取得しています。
ただ、誕生日はnullでした。

アプリの挙動として、入力したデータを保存するタイミングでfaccebookのタイムラインにも投稿したい。
で、投稿するソースがこちらを参考にしました。

タイムラインにもアプリから投稿した内容が表示されています。
twitterに投稿するという選択肢もありますが、アプリの内容からしても、faccebookの方が合っていると思い、今回はfacebook SDKを導入しました。
株式会社woodsmallの小林でした。
https://woodsmall.co.jp
コメント