java Android 共享意图选择器 - 与 Facebook/Twitter 社交媒体等共享文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10858009/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android Share Intent chooser - Sharing TEXT with Facebook/Twitter Social media etc
提问by tiptopjat
If I only send text, the Share Intent chooser DOES NOT give Facebook/Twitter as an option.
如果我只发送文本,则共享意图选择器不会将 Facebook/Twitter 作为选项。
Only Gmail, Skype and Evernote are options.
只有 Gmail、Skype 和 Evernote 是选项。
Here is my code
这是我的代码
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("plain/text");
shareIntent.putExtra(Intent.EXTRA_TEXT, text)
startActivity(Intent.createChooser(shareIntent, "Share using"));
I have tried different combination of setType() with no joy including "text/*", "text/html" and passing HTML text in the putExtra as follows:
我尝试了 setType() 的不同组合,但没有任何乐趣,包括“text/*”、“text/html”和在 putExtra 中传递 HTML 文本,如下所示:
shareIntent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml("<p>This is the text that will be shared.</p>"));
When I use "text/plain", Facebook comes up as an option but the text does not load up when selecting it. But the text does load for Twitter,email, SMS.
当我使用“文本/纯文本”时,Facebook 会作为一个选项出现,但在选择它时文本不会加载。但文本确实为 Twitter、电子邮件、SMS 加载。
Has anyone else encountered this problem?
有没有其他人遇到过这个问题?
When I share an image, there is no problem and Facebook along with other social media apps are available on the list.
当我分享一张图片时,没有问题,Facebook 和其他社交媒体应用程序都可以在列表中使用。
采纳答案by Orlymee
this depends upon what intent filters are defined by each of those apps.
For instance if I add intent-filter
android.intent.action.send
这取决于每个应用程序定义了哪些意图过滤器。
例如,如果我添加意图过滤器
android.intent.action.send
If I choose single image from Gallery my application will appear in the list. However if I choose multiple my application will not appear as I have not added the intent-filer for android.intent.action.send_multiple
如果我从图库中选择单个图像,我的应用程序将出现在列表中。但是,如果我选择多个我的应用程序将不会出现,因为我没有为android.intent.action.send_multiple
So it depends upon what intents facebook is filtering for. You need to see the release notes or help or developer pages for that.
所以这取决于 facebook 过滤的意图。您需要查看发行说明或帮助或开发人员页面。
回答by pedronveloso
Also it should be "text/plain" and not "plain/text" according to the documentation.
根据文档,它也应该是“text/plain”而不是“plain/text”。
回答by daniel uribe ayvar
The facebook issue is a limitation in facebook permissions. Use the facebook API
facebook 问题是 facebook 权限的限制。使用脸书 API
回答by Vaishali Sutariya
Share via Twitter :
通过推特分享:
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, (String)v.getTag(R.string.app_name));
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, (String)v.getTag(R.drawable.ic_launcher));
// for finding twitter package name ---- >>
// 用于查找 Twitter 包名称 ---- >>
PackageManager pm = v.getContext().getPackageManager();
List<ResolveInfo> activityList = pm.queryIntentActivities(shareIntent, 0);
for (final ResolveInfo app : activityList)
{
if ("com.twitter.android.PostActivity".equals(app.activityInfo.name))
{
final ActivityInfo activity = app.activityInfo;
final ComponentName name = new ComponentName(activity.applicationInfo.packageName, activity.name);
shareIntent.addCategory(Intent.CATEGORY_LAUNCHER);
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
shareIntent.setComponent(name);
v.getContext().startActivity(shareIntent);
break;
}
}
Share via Facebook
通过脸书分享
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,String)v.getTag(R.string.app_name));
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, (String)
v.getTag(R.drawable.ic_launcher));
// finding facebook package name
PackageManager pm = v.getContext().getPackageManager();
List<ResolveInfo> activityList = pm.queryIntentActivities(shareIntent, 0);
for (final ResolveInfo app : activityList)
{
if ((app.activityInfo.name).contains("facebook"))
{
final ActivityInfo activity = app.activityInfo;
final ComponentName name = new ComponentName(activity.applicationInfo.packageName, activity.name);
shareIntent.addCategory(Intent.CATEGORY_LAUNCHER);
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
shareIntent.setComponent(name);
v.getContext().startActivity(shareIntent);
break;
}
}
Share via Gmail
通过 Gmail 分享
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT(String)v.getTag(R.string.app_name));
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT(String)v.getTag(R.drawable.ic_launcher));
// finding gmail package name ---
PackageManager pm = v.getContext().getPackageManager();
List<ResolveInfo> activityList = pm.queryIntentActivities(shareIntent, 0);
for (final ResolveInfo app : activityList)
{
if ((app.activityInfo.name).contains("gmail"))
{
final ActivityInfo activity = app.activityInfo;
final ComponentName name = new ComponentName(activity.applicationInfo.packageName, activity.name);
shareIntent.addCategory(Intent.CATEGORY_LAUNCHER);
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
shareIntent.setComponent(name);
v.getContext().startActivity(shareIntent);
break;
}
}