通过 Facebook SDK,我向 Facebook 分享了一条消息。我可以成功分享我的消息,但是我没有看到任何登录和发布消息的对话框。当我点击我的分享按钮时,我的消息将在没有对话界面的情况下发布到 Facebook。那么,如何显示对话框并让我的用户点击发布按钮?
这是 mt 代码:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(TAG, "Try to create activity...");
mFacebook = new Facebook(FACEBOOK_APPID);
mAsyncRunner = new AsyncFacebookRunner(mFacebook);
if(!restoreCredentials(mFacebook)) {
Log.i(TAG, "Try to get token...");
mFacebook.authorize(this, PERMISSIONS, Facebook.FORCE_DIALOG_AUTH, new LoginDialogListener());
}
postFacebookMessage();
}
//
private void postFacebookMessage() {
Log.i(TAG, "Try to post message");
token = mFacebook.getAccessToken();
Log.i("token", "" + token);
Bundle parameters = new Bundle();
parameters.putString("access_token", token);
parameters.putString("message", "Text is test message.");
parameters.putString("name", "Chef Astro");
parameters.putString("link", "http://www.astrogempak.com.my");
parameters.putString("caption", "Caption");
parameters.putString("description", message);
parameters.putString("picture","http://a1.mzstatic.com/us/r30/Purple/v4/2d/7d/c3/2d7dc3ed-9e32-cd7b-adba-2a31637a8cf0/mzl.toeyydsk.175x175-75.jpg");
mAsyncRunner.request("me/feed", parameters, "POST", new WallPostRequestListener(), null);
}
//
class LoginDialogListener implements DialogListener {
public void onComplete(Bundle values) {
saveCredentials(mFacebook);
Log.i(TAG, "Credentials saved.");
}
public void onFacebookError(FacebookError error) {
showToast("Authentication with Facebook failed!");
}
public void onError(DialogError error) {
showToast("Authentication with Facebook failed!");
}
public void onCancel() {
showToast("Authentication with Facebook cancelled!");
}
}
//
public boolean saveCredentials(Facebook facebook) {
Editor editor = getApplicationContext().getSharedPreferences(KEY, Context.MODE_PRIVATE).edit();
editor.putString(TOKEN, facebook.getAccessToken());
editor.putLong(EXPIRES, facebook.getAccessExpires());
return editor.commit();
}
public boolean restoreCredentials(Facebook facebook) {
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences(KEY, Context.MODE_PRIVATE);
facebook.setAccessToken(sharedPreferences.getString(TOKEN, null));
facebook.setAccessExpires(sharedPreferences.getLong(EXPIRES, 0));
return facebook.isSessionValid();
}
//
public class WallPostRequestListener extends BaseRequestListener {
public void onComplete(final String response, final Object state) {
Log.d("Facebook-Example", "Got response: " + response);
String message = "<empty>";
try {
JSONObject json = Util.parseJson(response);
message = json.getString("message");
} catch (JSONException e) {
Log.w("Facebook-Example", "JSON Error in response");
} catch (FacebookError e) {
Log.w("Facebook-Example", "Facebook Error: " + e.getMessage());
}
final String text = "Your Wall Post: " + message;
FacebookActivity.this.runOnUiThread(new Runnable() {
public void run() {
Log.d(TAG, text);
}
});
closeActivity();
}
}
在上面的代码中,我希望当对话框显示时用户可以在文本框中写下他/她的建议,我在这里得到它。