通过 ACTION_SEND 从 Android 应用程序在 Facebook 上分享文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3515198/
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
Share Text on Facebook from Android App via ACTION_SEND
提问by Goddchen
I have an Android app and it supports sending text via other apps. It therefore uses the ACTION_SEND
intent and the EXTRA_TEXT
field. The chooser presents me with all apps that can handle such an intent. Those are Twitter, Email, ... and Facebook. But when I select Facebook it opens the browser and goes to the following page:
我有一个 Android 应用程序,它支持通过其他应用程序发送文本。因此,它使用ACTION_SEND
意图和EXTRA_TEXT
字段。选择器向我展示了所有可以处理这种意图的应用程序。这些是 Twitter、电子邮件、...和 Facebook。但是当我选择 Facebook 时,它会打开浏览器并转到以下页面:
http://m.facebook.com/sharer.php?u=mytext
http://m.facebook.com/sharer.php?u=mytext
It shows my text and the submit button. But when I press the submit button nothing happens. The page just loads again. I think maybe it is only possible to send URLs via the Facebook App. Could that be?
它显示了我的文本和提交按钮。但是当我按下提交按钮时,什么也没有发生。该页面刚刚再次加载。我想也许只能通过 Facebook 应用程序发送 URL。那可能吗?
Did anyone manage to send text via ACTION_SEND
through the Facebook Android app?
有没有人设法ACTION_SEND
通过 Facebook Android 应用程序发送文本?
采纳答案by Giulio Prisco
EDITED: with the new release of the official Facebook app for Android (July 14 2011) IT WORKS!!!
编辑:随着适用于 Android 的官方 Facebook 应用程序的新版本(2011 年 7 月 14 日),它可以工作了!!!
OLD: The examples above do not work if the user chooses the Facebook app for sharing, but they do work if the user chooses the Seesmic app to post to Facebook. I guess Seesmic have a better implementation of the Facebook API than Facebook!
旧:如果用户选择 Facebook 应用程序进行共享,则上述示例不起作用,但如果用户选择 Seesmic 应用程序发布到 Facebook,则它们确实有效。我猜 Seesmic 比 Facebook 有更好的 Facebook API 实现!
回答by ol_v_er
To make the Share work with the facebook app, you only need to have suply at least one link:
要使分享与 facebook 应用程序一起使用,您只需要提供至少一个链接:
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, "Wonderful search engine http://www.google.fr/");
startActivity(Intent.createChooser(intent, "Share with"));
This will show the correct sharing window but when you click on share, nothing happends (I also tried with the official Twitter App, it does not work).
这将显示正确的共享窗口,但是当您单击共享时,什么也没有发生(我也尝试过使用官方 Twitter 应用程序,但它不起作用)。
The only way I found to make the Facebook app sharing work is to share only a link with no text:
我发现使 Facebook 应用程序共享工作的唯一方法是只共享一个没有文本的链接:
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, "http://www.google.fr/");
startActivity(Intent.createChooser(intent, "Share with"));
It will show the following window and the Share button will work:
它将显示以下窗口,并且共享按钮将起作用:
Apparently it automatically takes an image and text from the link to populate the share.
显然,它会自动从链接中获取图像和文本来填充共享。
If you want to share only text, you will have to use the facebook api: https://github.com/facebook/facebook-android-sdk
如果您只想共享文本,则必须使用 facebook api:https: //github.com/facebook/facebook-android-sdk
回答by Loda
06/2013 :
06/2013 :
- This is a bug from Facebook, not your code
- Facebook will NOT fix this bug, they say it is "by design"that they broke the Android share system : https://developers.facebook.com/bugs/332619626816423
- use the SDK or share only URL.
- Tips: you could cheat a little using the web page title as text for the post.
- 这是 Facebook 的错误,而不是您的代码
- Facebook 不会修复这个错误,他们说他们破坏了 Android 共享系统是“设计使然”:https: //developers.facebook.com/bugs/332619626816423
- 使用 SDK 或仅共享 URL。
- 提示:您可以使用网页标题作为帖子的文本来作弊。
回答by khaintt
First you need query Intent to handler sharing option. Then use package name to filter Intent then we will have only one Intent that handler sharing option!
首先,您需要查询 Intent 到处理程序共享选项。然后使用包名来过滤 Intent 那么我们将只有一个处理程序共享选项的 Intent!
Share via Facebook
通过脸书分享
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Content to share");
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;
}
}
Bonus - Share via Twitter
奖金 - 通过 Twitter 分享
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Content to share");
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;
}
}
And if you want to find how to share via another sharing application, find it there Tép Blog - Advance share via Android
如果您想了解如何通过其他共享应用程序进行共享,请在那里找到Tép 博客 - 通过 Android 提前共享
回答by Joseph Lee
So I have a work around, but it assumes you have control over the page you're sharing...
所以我有一个解决方法,但它假设你可以控制你正在共享的页面......
If you format your EXTRA_TEXT like so...
如果你像这样格式化你的 EXTRA_TEXT ......
String myText = "Hey!\nThis is a neat pic!";
String extraText = "http://www.example.com/myPicPage.html?extraText=\n\n" + myText;
... then on non-Facebook apps, your text should appear something like this:
...然后在非 Facebook 应用程序上,您的文本应如下所示:
http://www.example.com/myPicPage.html?extraText=
Hey!
This is a neat pic!
http://www.example.com/myPicPage.html?extraText=
嘿!
这是一张整洁的照片!
Now if you update your website such that requests with the extraText query parameter return the contents of extraText in the page's meta data.
现在,如果您更新您的网站,以便使用 extraText 查询参数的请求返回页面元数据中 extraText 的内容。
<!-- Make sure to sanitize your inputs! e.g. http://xkcd.com/327/ -->
<meta name="title" content="Hey! this is a neat pic!">
Then when Facebook escapes that url to generate the dialog, it'll read the title meta data and embed it into your share dialog.
然后,当 Facebook 转义该 url 以生成对话框时,它会读取标题元数据并将其嵌入到您的共享对话框中。
I realize this is a pretty yuck solution, so take with a grain of salt...
我意识到这是一个非常糟糕的解决方案,所以请带上一粒盐......
回答by HXCaine
It appears that the Facebook app handles this intent incorrectly. The most reliable way seems to be to use the Facebook API for Android.
Facebook 应用程序似乎错误地处理了此意图。最可靠的方法似乎是使用 Facebook API for Android。
The SDK is at this link: http://github.com/facebook/facebook-android-sdk
SDK 位于此链接:http: //github.com/facebook/facebook-android-sdk
Under 'usage', there is this:
在“使用”下,有:
Display a Facebook dialog.
The SDK supports several WebView html dialogs for user interactions, such as creating a wall post. This is intended to provided quick Facebook functionality without having to implement a native Android UI and pass data to facebook directly though the APIs.
显示 Facebook 对话框。
SDK 支持多个用于用户交互的 WebView html 对话框,例如创建墙贴。这旨在提供快速的 Facebook 功能,而无需实现本机 Android UI 并通过 API 直接将数据传递给 Facebook。
This seems like the best way to do it -- display a dialog that will post to the wall. The only issue is that they may have to log in first
这似乎是最好的方法——显示一个将张贴到墙上的对话框。唯一的问题是他们可能必须先登录
回答by Vinod Joshi
Check this out : By this we can check activity results also....
// Open all sharing option for user
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, ShortDesc+" from "+BusinessName);
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, ShortDesc+" "+ShareURL);
sharingIntent.putExtra(Intent.EXTRA_TITLE, ShortDesc+" "+ShareURL);
startActivityForResult(Intent.createChooser(sharingIntent, "Share via"),1000);
/**
* Get the result when we share any data to another activity
* */
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode) {
case 1000:
if(resultCode == RESULT_OK)
Toast.makeText(getApplicationContext(), "Activity 1 returned OK", Toast.LENGTH_LONG).show();
else
Toast.makeText(getApplicationContext(), "Activity 1 returned NOT OK", Toast.LENGTH_LONG).show();
break;
case 1002:
if(resultCode == RESULT_OK)
Toast.makeText(getApplicationContext(), "Activity 2 returned OK", Toast.LENGTH_LONG).show();
break;
}// end switch
}// end onActivityResult
回答by Ram Bhawan Kushwaha
ShareDialog shareDialog = new ShareDialog(this);
if(ShareDialog.canShow(ShareLinkContent.class)) {
ShareLinkContent linkContent = new ShareLinkContent.Builder().setContentTitle(strTitle).setContentDescription(strDescription)
.setContentUrl(Uri.parse(strNewsHtmlUrl))
.build();
shareDialog.show(linkContent);
}
回答by MFQ
if you want to show text put #at the begging of the message you want it will share it as Hashtag
如果你想显示文本,在你想要的消息的乞求处输入#它将作为 Hashtag 共享它
回答by Joseph Woodward
It appears that it's a bug in the Facebook app that was reported in April 2011 and has still yet to be fixed by the Android Facebook developers.
这似乎是 Facebook 应用程序中的一个错误,该错误于 2011 年 4 月报告,但尚未由 Android Facebook 开发人员修复。
The only work around for the moment is to use their SDK.
目前唯一的解决方法是使用他们的 SDK。