在Android中,我如何使用Facebook应用程序而不是通过网络共享(意图)?

时间:2014-05-06 12:59:25

标签: java android facebook facebook-graph-api android-intent

到目前为止,在Android中我知道如何通过网络发布到用户的墙上,但我想,应用程序检测用户是否安装了facebook应用程序,如果是这样通过它发布。有谁知道怎么做?

到目前为止我的代码如下:

public class MainActivity extends Activity
{
    private static String FACEBOOK_APP_ID = "276023232498070";
    private static final String[] PERMISSIONS = 
            new String[] { "publish_stream","email","user_birthday","user_location" };

    private Facebook facebook;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        facebook = new Facebook(FACEBOOK_APP_ID);
        Bundle parameters = new Bundle();
        parameters.putString("app_id", "276023232498070");
        parameters.putString("link", "https://play.google.com/store/apps/details?id=myappistasty");
        parameters.putString("name", "This is the name of the link set in app.");
        parameters.putString("caption", "This is Text that is specified in bt the aoo");
        parameters.putString("picture", 
                "http://i.dailymail.co.uk/i/pix/2012/11/15/article-0-1609D0FF000005DC-373_964x641.jpg");

        // Posting my message, maybe here i could add paramterers like icon and a link etc..?
        facebook.dialog(MainActivity.this, "feed", parameters,new PostDialogListener());
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);
        facebook.authorizeCallback(requestCode, resultCode, data);
        Toast.makeText(getApplicationContext(), "Hello", Toast.LENGTH_LONG).show();

    }

    public abstract class BaseDialogListener implements DialogListener {
        @Override
        public void onFacebookError(FacebookError e) {
            e.printStackTrace();
        }
        @Override
        public void onError(DialogError e) {
            e.printStackTrace();
        }
        @Override
        public void onCancel() {
        }

    }

    public class PostDialogListener extends BaseDialogListener {
        @Override
        public void onComplete(Bundle values) {
            final String postId = values.getString("post_id");
            if (postId != null) {
                Log.e("","Message posted on the wall.");
                MainActivity.this.finish();
            } else {
                Log.e("","No message posted on the wall.");
                MainActivity.this.finish();
            }
        }
    }
}

2 个答案:

答案 0 :(得分:0)

您应该获得可以处理Intent.ACTION_SEND并找到Facebook应用的活动列表。

Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "In Android, how do I share (intent) using the Facebook application rather than via web?");
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "http://stackoverflow.com/questions/23495438");

PackageManager packageManager = getPackageManager();
List activityList = packageManager.queryIntentActivities(shareIntent, 0);
for (final ResolveInfo app : activityList) {
    if ((app.activityInfo.name).contain("facebook")) {
        final ActivityInfo activity = app.activityInfo;
        final ComponentName name = new ComponentName(activity.applicationInfo.packageName, activity.name);
        shareIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
        shareIntent.setComponent(name);
        startActivity(shareIntent);
        break;
    }
}

答案 1 :(得分:0)

执行此操作的首选方法是通过Android的Share Dialog:

https://developers.facebook.com/docs/android/share/#linkshare