【问题标题】:how to create Android intent to share on facebook?如何创建在 Facebook 上分享的 Android 意图?
【发布时间】:2014-08-31 14:05:47
【问题描述】:

我不明白哪里错了 我基本上是在尝试这样做,当我单击“fb”按钮时,应用程序通过包含链接在 facebook 上打开一个共享,但是一旦我测试,它会打开没有链接的 facebook 页面? 你可以帮帮我吗? 我想知道您是否还可以输入固定文本以及链接(如果可能的话,您可能会看到我的代码行)? 谢谢

这是代码

final Button button = (Button) findViewById(R.id.fb);
button.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {

        String urlToShare = "https://www.google.it/?gfe_rd=cr&ei=IysDVMyHPMjD8gesvYH4DA";
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("text/plain");
        // intent.putExtra(Intent.EXTRA_SUBJECT, "Foo bar"); // NB: has no effect!
        intent.putExtra(Intent.EXTRA_TEXT, urlToShare);

        // See if official Facebook app is found
        boolean facebookAppFound = false;
        List<ResolveInfo> matches = getPackageManager().queryIntentActivities(intent, 0);
        for (ResolveInfo info : matches) {
            if (info.activityInfo.packageName.toLowerCase().startsWith("com.facebook.katana")) {
                intent.setPackage(info.activityInfo.packageName);
                facebookAppFound = true;
                break;
            }
        }

        // As fallback, launch sharer.php in a browser
        if (!facebookAppFound) {
            String sharerUrl = "https://www.facebook.com/sharer/sharer.php?u=" + urlToShare;
            intent = new Intent(Intent.ACTION_VIEW, Uri.parse(sharerUrl));
        }

        startActivity(intent);

【问题讨论】:

    标签: android facebook button android-intent share


    【解决方案1】:
    The Facebook application does not handle either the EXTRA_SUBJECT or EXTRA_TEXT fields.
    

    这里是错误链接:https://developers.facebook.com/bugs/332619626816423

    问题是,如果您将 URL 放入 EXTRA_TEXT 字段中,它确实有效。这就像他们故意剥离任何文本

    编辑: 最新的 Facebook 版本不允许您使用意图共享文本。您必须使用 Facebook SDK 来做到这一点 - 为简单起见,请使用 Facebook SDK + Android Simple Facebook (https://github.com/sromku/android-simple-facebook)。

    【讨论】:

    【解决方案2】:

    以下代码适用于您可以共享的所有社交网络:

            Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
            sharingIntent.setType("text/plain");
            sharingIntent.putExtra(Intent.EXTRA_TEXT,urlToShare);
            startActivity(Intent.createChooser(sharingIntent,"Şhare"));
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-28
    • 1970-01-01
    相关资源
    最近更新 更多