简体   繁体   中英

Android facebook post on wall like twitter

I would like to create intent for facebook like this example for twitter

Intent tweetIntent = new Intent(Intent.ACTION_SEND);
            tweetIntent.putExtra(Intent.EXTRA_TEXT, "some text");
            tweetIntent.setType("text/plain");

            PackageManager packManager = Model.getInstance().getContext().getPackageManager();
            List<ResolveInfo> resolvedInfoList = packManager.queryIntentActivities(tweetIntent,  PackageManager.MATCH_DEFAULT_ONLY);

            boolean resolved = false;
            for(ResolveInfo resolveInfo: resolvedInfoList){
                if(resolveInfo.activityInfo.packageName.startsWith("com.twitter.android")){
                    tweetIntent.setClassName(
                        resolveInfo.activityInfo.packageName, 
                        resolveInfo.activityInfo.name );
                    resolved = true;
                    break;
                }
            }
            if(resolved){
                startActivity(tweetIntent);
            }else{
                Toast.makeText(Model.getInstance().getContext(), "Twitter app isn't found", Toast.LENGTH_SHORT).show();
            }    

I have read a lots of articles where I found out this way (intentIntent.putExtra(Intent.EXTRA_TEXT, "" )) is not possible for facebook. So my question how can I do achieve my goal ? I simple want to post text to user's wall.

According to FB new policy, it will not allow to prefilling of text, while sharing any post. User has to manually enter post content.

Reference: Video: https://developers.facebook.com/docs/apps/review/prefill

Article: Check for "2. Give people control" https://developers.facebook.com/policy/

Article: Check for "Step 2: Add Publishing Logic" https://developers.facebook.com/docs/android/share

  To just open user's FB so that the user can post.. You can try this. However if you want to post any text or link you need to use FB SDK and permissions. It is not possible via intents anymore.

Follow this Link

  Intent sendIntent = new Intent();
            sendIntent.setAction(Intent.ACTION_SEND);
            sendIntent.putExtra(Intent.EXTRA_TEXT, "Hiee.");
            sendIntent.setType("text/plain");
            sendIntent.setPackage("com.facebook.katana");
            try {
                startActivity(sendIntent);
            } catch (android.content.ActivityNotFoundException ex) {
                Toast.makeText(getApplicationContext(), "Install Facebook", 5000).show();
            }

After adding the facebook SDK ! in your Project

Look at the following simplified example code:

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import com.facebook.android.*;
import com.facebook.android.Facebook.DialogListener;

public class FacebookActivity extends Activity implements DialogListener,
        OnClickListener
{

private Facebook facebookClient;
private LinearLayout facebookButton;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.test);//my layout xml

    facebookButton = (LinearLayout)this.findViewById(R.id.Test_Facebook_Layout);

}

@Override
public void onComplete(Bundle values)
{

    if (values.isEmpty())
    {
        //"skip" clicked ?
        return;
    }

    // if facebookClient.authorize(...) was successful, this runs
    // this also runs after successful post
    // after posting, "post_id" is added to the values bundle
    // I use that to differentiate between a call from
    // faceBook.authorize(...) and a call from a successful post
    // is there a better way of doing this?
    if (!values.containsKey("post_id"))
    {
        try
        {
            Bundle parameters = new Bundle();
            parameters.putString("message", "this is a test");// the message to post to the wall
            facebookClient.dialog(this, "stream.publish", parameters, this);// "stream.publish" is an API call
        }
        catch (Exception e)
        {
            // TODO: handle exception
            System.out.println(e.getMessage());
        }
    }
}

@Override
public void onError(DialogError e)
{
    System.out.println("Error: " + e.getMessage());
}

@Override
public void onFacebookError(FacebookError e)
{
    System.out.println("Error: " + e.getMessage());
}

@Override
public void onCancel()
{
}

@Override
public void onClick(View v)
{
    if (v == facebookButton)
    {
        facebookClient = new Facebook();
        // replace APP_API_ID with your own
        facebookClient.authorize(this, APP_API_ID,
            new String[] {"publish_stream", "read_stream", "offline_access"}, this);
    }
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM