Android Facebook Integration - Predefined Wall Pos

2019-08-07 19:10发布

问题:

I want to add functionality to my app where a user can share with their friends that they are using my app. I want the post to have a predefined message, but all I can get working is a regular post from my app. Is there anyway I can do this and maybe with an image as well. but that's not as important.. Any ideas?

Here's where I handle everything with Facebook (Logging in AND Posting):

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);
}
}

回答1:

Suppose that you want to send message to Facebook on button click.

@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();
                    }
                });
            }
        }
    });

    ...
}

And put this method in your activity:

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);
}

And add this class:

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);
    }
}

And this:

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
    }
}

I think that's all :)



回答2:

I simply use:

  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);

  }

And for the button in the 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" />

It will work for WhatsApp, text messages, Facebook wall posts etc...in this case with the predefined text: "This App is cool!"