在Facebook和其他社交媒体上分享android

Jan*_*ana 5 java android facebook android-intent

我想通过Facebook其他社交媒体(Google+,Twitter,...)分享一个简单的文字

我的初始代码看起来像:

Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_SUBJECT, "Test Subject");
shareIntent.putExtra(Intent.EXTRA_TITLE, "Test Title");
shareIntent.putExtra(Intent.EXTRA_TEXT, "Only link visible in FB: http://www.google.com/");
startActivity(Intent.createChooser(shareIntent, "Test Intent"));
Run Code Online (Sandbox Code Playgroud)

如果我没有弄错的话,Facebook允许我只分享指定的链接(或某种网站的摘录)EXTRA_TEXT.

虽然共享链接很好,但我也希望与它共享自定义文本,因此我将我的项目链接到facebook SDK并设法创建更多自定义共享体验.然而,由于分享到Facebook它不再通过Intent完成,我丢失了我Intent.createChooser向用户展示众所周知的共享选项列表.

我的问题是我可以使用默认值Intent.createChooser但在Facebook图标选择中使用自定义共享吗?如果没有,那么其他选择是什么?
谢谢!

Jan*_*ana 0

我找到了解决方案。

正如我在原来的问题中已经提到的,我按照FB 上的说明Activity将 Facebook 自定义共享(通过使用 Facebook SDK)封装到一个类中。ShareFacebook

然后我用它ShareFacebook ActivityIntent替换原来的Intent(通常由Intent.createChooser),如下所示:

String myPackageName = getPackageName();
String[] blacklist = new String[]{"com.pinterest", "com.tunewiki.lyricplayer.android", "com.dropbox.android", "com.google.zxing.client.android", "com.adobe.reader", "com.google.android.apps.docs", "flipboard.app"};
Set<Integer> usedSharingApps = new HashSet<Integer>();
int facebookHash1 = "com.facebook.katana".hashCode();
int facebookHash2 = "com.facebook.wakizashi".hashCode();

// share result
// By default intent only link is possible to share on facebook                         

    List<Intent> targetedShareIntents = new ArrayList<Intent>();

    Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
    shareIntent.setType("text/plain");
List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(shareIntent, 0);
if (!resInfo.isEmpty()){
    for (ResolveInfo resolveInfo : resInfo) {
        String packageName = resolveInfo.activityInfo.packageName;

        Intent targetShareIntent;
        int appId = packageName.hashCode();
        if (packageName.equals(myPackageName) 
            || Arrays.asList(blacklist).contains(packageName) /* skip some other packages */
            || usedSharingApps.contains(appId)) {
            continue;
        } else if (packageName.equals("com.facebook.katana") || packageName.equals("com.facebook.wakizashi")) {
            usedSharingApps.add(appId);
            Intent targetShareIntentTmp = new Intent(getApplicationContext(), ShareFacebook.class);
            targetShareIntentTmp.addCategory("android.intent.category.ALTERNATIVE");

            targetShareIntentTmp.setAction(Intent.ACTION_SEND);
            targetShareIntentTmp.setType("text/plain");
            targetShareIntentTmp.setPackage(myPackageName);
            targetShareIntentTmp.setComponent(new ComponentName(getPackageName(), ShareFacebook.class.getName()));
            targetShareIntentTmp.putExtra(Intent.EXTRA_SUBJECT, "FacebookDialog Description");
            targetShareIntentTmp.putExtra(Intent.EXTRA_TITLE, "FacebookDialog Name");
            targetShareIntentTmp.putExtra(Intent.EXTRA_TEXT, "http://stackoverflow.com");

            targetShareIntent = new LabeledIntent(targetShareIntentTmp, myPackageName, R.string.facebook, R.drawable.fb_logo_blue);

        } else {
            usedSharingApps.add(appId);
            targetShareIntent = new Intent();   

            targetShareIntent.setAction(Intent.ACTION_SEND);
            targetShareIntent.setType("text/plain");
            targetShareIntent.setPackage(packageName);

            targetShareIntent.putExtra(Intent.EXTRA_SUBJECT, "SUBJECT visible by mail or sms app");
            targetShareIntent.putExtra(Intent.EXTRA_TITLE, "TITLE - visible anywhere?");
            targetShareIntent.putExtra(Intent.EXTRA_TEXT, "All this text visible in every intent http://www.google.com/");
        }
        targetedShareIntents.add(targetShareIntent);
    }
}               

if (!usedSharingApps.contains(facebookHash1) && !usedSharingApps.contains(facebookHash2)) {
    // user does not have facebook app but we still want to share since it took us such effort to use facebook sharing!

    Intent targetShareIntentTmp = new Intent(getApplicationContext(), ShareFacebook.class);
    targetShareIntentTmp.addCategory("android.intent.category.ALTERNATIVE");

    targetShareIntentTmp.setAction(Intent.ACTION_SEND);
    targetShareIntentTmp.setType("text/plain");
    targetShareIntentTmp.setPackage(myPackageName);
    targetShareIntentTmp.setComponent(new ComponentName(getPackageName(), ShareFacebook.class.getName()));
    targetShareIntentTmp.putExtra(Intent.EXTRA_SUBJECT, "FacebookDialog Description");
    targetShareIntentTmp.putExtra(Intent.EXTRA_TITLE, "FacebookDialog Name");
    targetShareIntentTmp.putExtra(Intent.EXTRA_TEXT, "http://stackoverflow.com");

    Intent targetShareIntent = new LabeledIntent(targetShareIntentTmp, myPackageName, R.string.facebook, R.drawable.fb_logo_blue);
    targetedShareIntents.add(targetShareIntent);
}

Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(0), "Select app to share");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[]{}));
startActivity(chooserIntent);           
Run Code Online (Sandbox Code Playgroud)

在我添加的清单文件中:

    <activity
        android:name="your.package.ShareFacebook" >
        <intent-filter>
            <action android:name="android.intent.action.SEND" />                
            <category android:name="android.intent.category.ALTERNATIVE" />
            <data android:mimeType="text/plain" />
        </intent-filter>
    </activity>
Run Code Online (Sandbox Code Playgroud)

修改清单非常重要,否则我会收到此错误:“找不到 Intent 的活动”

我还下载了Facebook 徽标并将其复制到我的项目资源中,名称为“fb_logo_blue.png”(文件名必须仅包含 a-z0-9_)。

对于这两行额外的行(FacebookDialog 描述 + FacebookDialog 名称),我必须链接到 FB SDK,并消磨了很多时间!好吧,至少现在我可以在没有安装 Facebook 应用程序的手机上分享。

我在 Facebook 上的结果:

在此输入图像描述