分享至Facebook,通过共享意图的android微博 [英] Sharing to facebook, twitter via share intent android
问题描述
目前我正在试图分享文字到Gmail,Facebook和Twitter。我使用份额的意图的方法来共享。下面是code I用于共享
I am currently trying to share text to gmail, facebook and twitter. I am using share intent method to share. Below is the code i used to share
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"));
以上code只能说明,如Gmail和Skype的股权。为什么Facebook和Twitter选项不显示在列表中,即使我已经安装在我的设备的Facebook和Twitter的应用程序?
The above code only show share options like gmail and skype. Why is facebook and twitter options not shown in the list even though i had the facebook and twitter application installed in my device?
推荐答案
我不知道是否有人还在读这一点,但如果人们仍然在寻找我发现了一个简单的答案:
I don't know if anybody still reads this, but if people are still looking I found an easier answer:
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这种方法搜索/平,可以使用其他MIME类型,通过这些类型搜索或使用/ * /通过各类搜索:
And the getShareIntent method (this method searches through the mime type text/plain, you can use other mime types to search through those types or use /*/ to search through all types:
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;
}
如果你不想使用此方法,你可以创建这样的意图:
If you don't want to use this method you can just create Intents like this:
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");
这篇关于分享至Facebook,通过共享意图的android微博的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!