gpt4 book ai didi

android - 通过共享 Intent android 共享到 facebook、twitter

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

我目前正在尝试将文本分享到 gmail、facebook 和 twitter。我正在使用共享 Intent 方法来共享。下面是我用来分享的代码

Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("plain/text");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, "My text");
startActivity(Intent.createChooser(sharingIntent,"Share using"));

上面的代码只显示了 gmail 和 skype 等共享选项。为什么 facebook 和 twitter 选项没有显示在列表中,即使我的设备中安装了 facebook 和 twitter 应用程序?

最佳答案

我不知道是否还有人在读这篇文章,但如果有人还在看,我找到了一个更简单的答案:

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

Intent facebookIntent = getShareIntent("facebook", "subject", "text");
if(facebookIntent != null)
targetedShareIntents.add(facebookIntent);

Intent twitterIntent = getShareIntent("twitter", "subject", "text");
if(twitterIntent != null)
targetedShareIntents.add(twitterIntent);

Intent gmailIntent = getShareIntent("gmail", "subject", "text");
if(gmailIntent != null)
targetedShareIntents.add(gmailIntent);

Intent chooser = Intent.createChooser(targetedShareIntents.remove(0), "Delen");

chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[]{}));

startActivity(chooser);

getShareIntent 方法(该方法搜索 mime 类型 text/plain,您可以使用其他 mime 类型搜索这些类型或使用/*/搜索所有类型:

private Intent getShareIntent(String type, String subject, String text) 
{
boolean found = false;
Intent share = new Intent(android.content.Intent.ACTION_SEND);
share.setType("text/plain");

// gets the list of intents that can be loaded.
List<ResolveInfo> resInfo = getActivity().getPackageManager().queryIntentActivities(share, 0);
System.out.println("resinfo: " + resInfo);
if (!resInfo.isEmpty()){
for (ResolveInfo info : resInfo) {
if (info.activityInfo.packageName.toLowerCase().contains(type) ||
info.activityInfo.name.toLowerCase().contains(type) ) {
share.putExtra(Intent.EXTRA_SUBJECT, subject);
share.putExtra(Intent.EXTRA_TEXT, text);
share.setPackage(info.activityInfo.packageName);
found = true;
break;
}
}
if (!found)
return null;

return share;
}
return null;
}

如果你不想使用这个方法,你可以像这样创建 Intents:

Intent normalIntent = new Intent(Intent.ACTION_SEND);
normalIntent.setType("text/plain");
normalIntent.setPackage("com.katana.facebook"); // I just know the package of Facebook, the rest you will have to search for or use my method.
normalIntent.putExtra(Intent.EXTRA_TEXT, "The text you want to share to Facebook");

关于android - 通过共享 Intent android 共享到 facebook、twitter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13286358/

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