我想通过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"));
如果我没弄错的话,Facebook允许我只分享通过EXTRA_TEXT
指定的链接(或某种网站摘录)。
虽然分享链接很好,但我也希望与它共享自定义文本,因此我将我的项目链接到facebook SDK并设法创建更多自定义共享体验。然而,由于分享到Facebook不再通过Intent完成,我丢失了Intent.createChooser
,向用户显示了众所周知的共享选项列表。
我的问题是我可以使用默认的Intent.createChooser
但是在Facebook图标选择中使用自定义共享吗?如果没有,那么其他选择是什么?
谢谢!
答案 0 :(得分:0)
我找到了解决方案。
正如我在原始问题中已经提到的,我按照instructions on FB将我的Facebook自定义共享(使用Facebook SDK)封装到Activity
课程ShareFacebook
中。
然后我将此ShareFacebook Activity
用作Intent
并替换原始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);
我在清单文件中添加了:
<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>
修改清单非常重要,否则我收到此错误:&#34;没有为Intent找到活动&#34;
我还下载了Facebook logo并将其复制到我的项目资源名称&#34; fb_logo_blue.png&#34; (文件名必须只包含a-z0-9 _)。
对于这两个额外的行(FacebookDialog描述+ FacebookDialog名称),我不得不链接到FB SDK并杀了这么多个小时!好吧,至少现在我可以在没有安装Facebook应用程序的手机上分享。
我在Facebook上的结果: