Skip to content Skip to sidebar Skip to footer

Android Facebook Integration - Predefined Wall Post

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

Solution 1:

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

@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {

    ...

    Button sharebutton = (Button) findViewById(R.id.share_button);
    sharebutton.setOnClickListener(newOnClickListener() {
        @OverridepublicvoidonClick(View v) {
            if (facebook.isSessionValid()) {
                postFacebookMessage();
            } else {
                facebook.authorize(YourActivity.this, newString[] {"publish_stream"}, newFacebookAuthListener() {
                    @OverridepublicvoidonComplete(Bundle values) {
                        postFacebookMessage();
                    }
                });
            }
        }
    });

    ...
}

And put this method in your activity:

privatevoidpostFacebookMessage() {
    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;

publicclassFacebookPostListenerimplementsRequestListener {
    privatestaticfinalStringTAG="FacebookPostListener";

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

    @OverridepublicvoidonFacebookError(FacebookError e, final Object state) {
        Log.e(TAG, e.getMessage(), e);
    }

    @OverridepublicvoidonFileNotFoundException(FileNotFoundException e, final Object state) {
        Log.e(TAG, e.getMessage(), e);
    }

    @OverridepublicvoidonIOException(IOException e, final Object state) {
        Log.e(TAG, e.getMessage(), e);
    }

    @OverridepublicvoidonMalformedURLException(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;

publicclassFacebookAuthListenerimplementsDialogListener {

    privatestatic final StringTAG = FacebookAuthListener.class.getSimpleName();

    @OverridepublicvoidonFacebookError(FacebookError e) {
        Log.e(TAG, e.getMessage(), e);
    }

    @OverridepublicvoidonError(DialogError e) {
        Log.e(TAG, e.getMessage(), e);
    }

    @OverridepublicvoidonComplete(Bundle values) {
    }

    @OverridepublicvoidonCancel() {
        // Do nothing
    }
}

I think that's all :)

Solution 2:

I simply use:

publicvoidinviteSomeone(View view) {

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

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

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

  }

And for the button in the XML:

<!--for the Invite button--><Buttonandroid: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!"

Post a Comment for "Android Facebook Integration - Predefined Wall Post"