Android Facebook集成 - 预定义的Wall Post

时间:2011-11-23 06:07:55

标签: java android facebook integration

我想在我的应用中添加功能,用户可以与朋友分享他们正在使用我的应用。我希望帖子有一个预定义的消息,但我可以开始工作的是我的应用程序的常规帖子。无论如何,我可以做到这一点,也许还有一个图像。但那不重要..任何想法?

这是我用Facebook处理所有内容的地方(登录和发布):

public class FacebookSSO extends Activity {

Facebook facebook = new Facebook("APP_ID");
private SharedPreferences mPrefs;
private static final String TAG = "FacebookConnect";
private static final String MSG = "Signed In Using MNWV For Android - Check It Out!";    

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    /*
     * Get existing access_token if any
     */
    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);
    }

    /*
     * Only call authorize if the access_token has expired.
     */
    if(!facebook.isSessionValid()) {

            facebook.dialog(this, "oauth", new DialogListener() {
          @Override
          public void onComplete(Bundle values) {  
              SharedPreferences.Editor editor = mPrefs.edit();
              editor.putString("access_token", facebook.getAccessToken());
              editor.putLong("access_expires", facebook.getAccessExpires());
              editor.commit();                       
              Bundle parameters = new Bundle();
              parameters.putString(TAG, MSG);// the message to post to the wall                    

              facebook.dialog(FacebookSSO.this, "stream.publish", parameters, new DialogListener() { 
                  @Override
                public void onComplete(Bundle values) {  
                    SharedPreferences.Editor editor = mPrefs.edit();
                    editor.putString("access_token", facebook.getAccessToken());
                    editor.putLong("access_expires", facebook.getAccessExpires());
                    editor.commit();  

                       Intent in = new Intent(FacebookSSO.this, MNWVMainScreen.class);
                         startActivity(in);
                }

                @Override
                public void onFacebookError(FacebookError error) {}

                @Override
                public void onError(DialogError e) {}

                @Override
                public void onCancel() {}

              });
          }

          @Override
          public void onFacebookError(FacebookError error) {}

          @Override
          public void onError(DialogError e) {}

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

            }
} 

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    facebook.authorizeCallback(requestCode, resultCode, data);
}
}

2 个答案:

答案 0 :(得分:6)

假设您想在点击按钮时向Facebook发送消息。

@Override
protected void onCreate(Bundle savedInstanceState) {

    ...

    Button sharebutton = (Button) findViewById(R.id.share_button);
    sharebutton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (facebook.isSessionValid()) {
                postFacebookMessage();
            } else {
                facebook.authorize(YourActivity.this, new String[] {"publish_stream"}, new FacebookAuthListener() {
                    @Override
                    public void onComplete(Bundle values) {
                        postFacebookMessage();
                    }
                });
            }
        }
    });

    ...
}

将此方法放入您的活动中:

private void postFacebookMessage() {
    AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook);
    Bundle params = new Bundle();
    params.putString("message", "I am using great App!");
    params.putString("picture", "http://mysite.com/logo.jpg");
    mAsyncRunner.request("me/feed", params, "POST", new FacebookPostListener(), null);
}

并添加此类:

import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;

import android.util.Log;

import com.facebook.android.FacebookError;
import com.facebook.android.AsyncFacebookRunner.RequestListener;

public class FacebookPostListener implements RequestListener {
    private static final String TAG = "FacebookPostListener";

    @Override
    public void onComplete(final String response, final Object state) {
        Log.d(TAG, "Facebook published the post. Got response: " + response);
    }

    @Override
    public void onFacebookError(FacebookError e, final Object state) {
        Log.e(TAG, e.getMessage(), e);
    }

    @Override
    public void onFileNotFoundException(FileNotFoundException e, final Object state) {
        Log.e(TAG, e.getMessage(), e);
    }

    @Override
    public void onIOException(IOException e, final Object state) {
        Log.e(TAG, e.getMessage(), e);
    }

    @Override
    public void onMalformedURLException(MalformedURLException e, final Object state) {
        Log.e(TAG, e.getMessage(), e);
    }
}

而且:

import android.os.Bundle;
import android.util.Log;

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

public class FacebookAuthListener implements DialogListener {

    private static final String TAG = FacebookAuthListener.class.getSimpleName();

    @Override
    public void onFacebookError(FacebookError e) {
        Log.e(TAG, e.getMessage(), e);
    }

    @Override
    public void onError(DialogError e) {
        Log.e(TAG, e.getMessage(), e);
    }

    @Override
    public void onComplete(Bundle values) {
    }

    @Override
    public void onCancel() {
        // Do nothing
    }
}

我认为这就是全部:)

答案 1 :(得分:0)

我只是使用:

  public void inviteSomeone(View view) {

    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("text/plain");

    Intent chooser = Intent.createChooser(intent, "Pick your app");

    String shareSub = "This App is cool!";
    intent.putExtra(Intent.EXTRA_TEXT, shareSub);

  }

对于XML中的按钮:

<!--for the Invite button-->
<Button
    android:id="@+id/btnInvite"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:onClick="inviteSomeone"
    android:text="@string/btnInvite" />

它将适用于WhatsApp,短信,Facebook墙贴等...在这种情况下,带有预定义的文本:“此应用非常酷!”