gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:26:16 25 4
gpt4 key购买 nike

我想通过 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 并设法创建更多自定义共享体验。但是,由于不再通过 Intent 共享到 Facebook,我丢失了 Intent.createChooser,它向用户显示了众所周知的共享选项列表。

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

最佳答案

我找到了解决方案。

正如我在最初的问题中提到的那样,我将我的 Facebook 自定义共享(通过使用 Facebook SDK)封装到 ActivityShareFacebook 中,方法是遵循 instructions on FB .

然后我将这个 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);

在我添加的 list 文件中:

    <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>

修改 list 非常重要,否则我会收到此错误:“找不到 Intent 的 Activity ”

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

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

我在 Facebook 上的结果:

enter image description here

关于java - android 在 Facebook 和其他社交媒体上分享,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22600040/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com