1

我想在我的应用中实现 Facebook 集成。在这个时间点,我有登录和发布到墙上的功能,但我拥有的墙帖只是像简单的墙帖。

我想实现这一点。就像在每场比赛中一样,他们有这种 facebook feed..

这是我目前的代码..

package com.example.facebooktrial;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.facebook.android.AsyncFacebookRunner;
import com.facebook.android.DialogError;
import com.facebook.android.Facebook;
import com.facebook.android.Facebook.DialogListener;
import com.facebook.android.FacebookError;

@SuppressWarnings("deprecation")
public class AndroidFacebookConnectActivity extends Activity {
    Button btnFbLogin;
    Button btnPostToWall;


    // Your Facebook APP ID
    private static String APP_ID = "593769430655402"; // Replace your App ID here

    // Instance of Facebook Class
    private Facebook facebook;
    private AsyncFacebookRunner mAsyncRunner;
    String FILENAME = "AndroidSSO_data";
    private SharedPreferences mPrefs;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnFbLogin = (Button) findViewById(R.id.btnFbLogin);
        btnPostToWall = (Button) findViewById(R.id.btnFbPost);

        facebook = new Facebook(APP_ID);
        mAsyncRunner = new AsyncFacebookRunner(facebook);

        btnFbLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    loginToFacebook();
                }
        });
        btnPostToWall.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                postToWall();
            }
        });


    }
    @SuppressWarnings("deprecation")
    public void loginToFacebook() {
        mPrefs = getPreferences(MODE_PRIVATE);
        String access_token = mPrefs.getString("access_token", null);
        long expires = mPrefs.getLong("access_expires", 0);

        if (access_token != null) {
            facebook.setAccessToken(access_token);
        }

        if (expires != 0) {
            facebook.setAccessExpires(expires);
        }

        if (!facebook.isSessionValid()) {
            facebook.authorize(this,
                    new String[] { "email", "publish_stream" },
                    new DialogListener() {

                        @Override
                        public void onCancel() {
                            // Function to handle cancel event
                        }

                        @Override
                        public void onComplete(Bundle values) {
                            // Function to handle complete event
                            // Edit Preferences and update facebook acess_token
                            SharedPreferences.Editor editor = mPrefs.edit();
                            editor.putString("access_token",
                                    facebook.getAccessToken());
                            editor.putLong("access_expires",
                                    facebook.getAccessExpires());
                            editor.commit();
                        }

                        @Override
                        public void onError(DialogError error) {
                            // Function to handle error

                        }

                        @Override
                        public void onFacebookError(FacebookError fberror) {
                            // Function to handle Facebook errors

                        }

                    });
        }
    }

    @SuppressWarnings("deprecation")
    public void postToWall() {
        // post on user's wall.
        facebook.dialog(this, "feed", new DialogListener() {

            @Override
            public void onFacebookError(FacebookError e) {
            }

            @Override
            public void onError(DialogError e) {
            }

            @Override
            public void onComplete(Bundle values) {
            }

            @Override
            public void onCancel() {
            }
        });

    }
}
4

1 回答 1

1

我找到了解决方案。只需使用一个捆绑包,您将在其中存储所有必要的信息,如图片、名称、链接等。之后,将该捆绑包作为参数包含在 Facebook 对话框中。

 @SuppressWarnings("deprecation")
    public void postToWall() {
        // post on user's wall.

        Bundle params = new Bundle();
        params.putString("name", "Check it out, I am playing FLIP game!");
        params.putString("caption", "Come on FLIP with me");
        params.putString("description", "FLIP!");
        params.putString("picture", "http://www.rawk.com/media/images/uploaded/products/2099/flip-hkd-black-complete-skateboard.3043.full.jpg");
        facebook.dialog(this, "feed",params, new DialogListener() {

            @Override
            public void onFacebookError(FacebookError e) {
            }

            @Override
            public void onError(DialogError e) {
            }

            @Override
            public void onComplete(Bundle values) {
            }

            @Override
            public void onCancel() {
            }
        });

    }
于 2013-08-04T17:30:45.770 回答