如何使用facebook SDK更新Facebook的状态?

时间:2015-01-23 03:22:06

标签: android facebook

我正在尝试用照片更新我的脸书状态。我使用了sharedialog,但是在墙上的帖子的实际描述之前显示了照片。 那么如何直接更新我的状态并将照片全部发布在墙上?

1 个答案:

答案 0 :(得分:0)

通过以下代码:

import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.app.ProgressDialog;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
import com.facebook.android.AsyncFacebookRunner;
import com.facebook.android.BaseRequestListener;
import com.facebook.android.Facebook;
import com.facebook.android.SessionStore;

public class TestPost extends Activity{
private Facebook mFacebook;
private CheckBox mFacebookCb;
private ProgressDialog mProgress;
private Handler mRunOnUi = new Handler();

private static final String APP_ID = "app id here";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.post);

    final EditText reviewEdit = (EditText) findViewById(R.id.revieew);
    mFacebookCb = (CheckBox) findViewById(R.id.cb_facebook);

    mProgress   = new ProgressDialog(this);

    mFacebook   = new Facebook(APP_ID);

    SessionStore.restore(mFacebook, this);

    if (mFacebook.isSessionValid()) {
        mFacebookCb.setChecked(true);

        String name = SessionStore.getName(this);
        name        = (name.equals("")) ? "Unknown" : name;

        mFacebookCb.setText("  Facebook  (" + name + ")");
    }

    ((Button) findViewById(R.id.button1)).setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            String review = reviewEdit.getText().toString();

            if (review.equals("")) return;

            if (mFacebookCb.isChecked()) postToFacebook(review);
        }
    });
}

private void postToFacebook(String review) {    
    mProgress.setMessage("Posting ...");
    mProgress.show();

    AsyncFacebookRunner mAsyncFbRunner = new AsyncFacebookRunner(mFacebook);

    Bundle params = new Bundle();           
    params.putString("message", review);
    params.putString("picture", "http://twitpic.com/show/thumb/6hqd44");

    mAsyncFbRunner.request("me/feed", params, "POST", new WallPostListener());
}

private final class WallPostListener extends BaseRequestListener {
    public void onComplete(final String response) {
        mRunOnUi.post(new Runnable() {
            @Override
            public void run() {
                mProgress.cancel();

                Toast.makeText(TestPost.this, "Posted to Facebook", Toast.LENGTH_SHORT).show();
            }
        });
    }
}
}