Android 和 Facebook 共享意图
我正在开发一个 Android 应用程序,并且有兴趣了解如何使用 Android 的共享意图在应用程序内更新应用程序用户的状态。
浏览过 Facebook 的 SDK 后,这似乎很容易做到,但是我很想允许用户通过常规的共享意图弹出窗口来做到这一点?在这里看到:
我已经尝试了通常的共享意图代码,但这似乎不再为 Facebook 工作。
public void invokeShare(Activity activity, String quote, String credit) {
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, activity.getString(R.string.share_subject));
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Example text");
activity.startActivity(Intent.createChooser(shareIntent, activity.getString(R.string.share_title)));
}
更新: 经过更多挖掘后,看来这是 Facebook 应用程序的一个错误,尚未解决! (facebook bug) 同时,看起来我只是必须忍受负面的“分享不起作用!!!”评论。干杯脸书:*(
I'm developing an Android app and am interested to know how you can update the app user's status from within the app using Android's share intents.
Having looked through Facebook's SDK it appears that this is easy enough to do, however I'm keen to allow the user to do it via the regular Share Intent pop up window? seen here:
I have tried the usual share intent code, however this no longer appears to work for Facebook.
public void invokeShare(Activity activity, String quote, String credit) {
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, activity.getString(R.string.share_subject));
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Example text");
activity.startActivity(Intent.createChooser(shareIntent, activity.getString(R.string.share_title)));
}
UPDATE:
Having done more digging, it looks as though it's a bug with Facebook's app that has yet to be resolved! (facebook bug) For the mean time it looks like I'm just going to have to put up with the negative "Sharing doesn't work!!!" reviews. Cheers Facebook :*(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
显然 Facebook 不再(截至 2014 年)允许您自定义共享屏幕,无论您是否只是打开 sharer.php URL 或以更专门的方式使用 Android 意图。例如,请参阅以下答案:
无论如何,使用简单的 Intents,您仍然可以共享网址,但不能共享任何默认文本,正如 billynomates 评论的< /a>. (此外,如果您没有要共享的 URL,只需使用空的“写帖子”(即状态更新)对话框启动 Facebook 应用程序也同样简单;使用下面的代码,但省略
EXTRA_TEXT
。)这是我发现的最佳解决方案不涉及使用任何 Facebook SDK。
此代码直接打开官方 Facebook 应用程序(如果已安装),否则返回到在浏览器中打开 sharer.php。 (这个问题中的大多数其他解决方案都会弹出一个巨大的“使用…完成操作”对话框 这根本不是最佳的!)
(关于
com.facebook.katana
包名,参见MatheusJardimB 的评论。)结果如下所示我的 Nexus 7 (Android 4.4) 安装了 Facebook 应用程序:
Apparently Facebook no longer (as of 2014) allows you to customise the sharing screen, no matter if you are just opening sharer.php URL or using Android intents in more specialised ways. See for example these answers:
Anyway, using plain Intents, you can still share a URL, but not any default text with it, as billynomates commented. (Also, if you have no URL to share, just launching Facebook app with empty "Write Post" (i.e. status update) dialog is equally easy; use the code below but leave out
EXTRA_TEXT
.)Here's the best solution I've found that does not involve using any Facebook SDKs.
This code opens the official Facebook app directly if it's installed, and otherwise falls back to opening sharer.php in a browser. (Most of the other solutions in this question bring up a huge "Complete action using…" dialog which isn't optimal at all!)
(Regarding the
com.facebook.katana
package name, see MatheusJardimB's comment.)The result looks like this on my Nexus 7 (Android 4.4) with Facebook app installed:
这是错误链接:https://developers.facebook.com/bugs/332619626816423
谢谢@billynomates:
Here is bug link: https://developers.facebook.com/bugs/332619626816423
Thanks @billynomates:
通常的方法
创建您所要求的内容的通常方法是简单地执行以下操作:
这对我来说没有任何问题。
替代方法(也许)
这样做的潜在问题是,您还允许通过电子邮件、短信等发送消息。以下代码是我正在使用的代码在一个应用程序中,允许用户使用 Gmail 向我发送电子邮件。我猜你可以尝试更改它以使其仅适用于 Facebook。
我不确定它如何响应任何错误或异常(我猜测如果未安装 Facebook,就会发生这种情况),因此您可能需要对其进行一些测试。
The usual way
The usual way to create what you're asking for, is to simply do the following:
This works without any issues for me.
The alternative way (maybe)
The potential problem with doing this, is that you're also allowing the message to be sent via e-mail, SMS, etc. The following code is something I'm using in an application, that allows the user to send me an e-mail using Gmail. I'm guessing you could try to change it to make it work with Facebook only.
I'm not sure how it responds to any errors or exceptions (I'm guessing that would occur if Facebook is not installed), so you might have to test it a bit.
我发现您只能共享文本或图像,而不能同时使用
意图
。下面的代码仅共享图像(如果存在),或者仅共享文本(如果图像不存在)。如果您想共享两者,则需要使用此处的 Facebook SDK。如果您使用其他解决方案代替下面的代码,不要忘记指定包名称 com.facebook.lite ,这是 Facebook Lite 的包名称。我还没有测试过,但如果您也想定位的话,com.facebook.orca 是 Facebook Messenger 的包名称。
您可以添加更多与 WhatsApp、Twitter ...
要从文件中获取Uri,请使用以下类:
要编写FileProvider,请使用此链接:https://github.com/codepath/android_guides/wiki/Sharing-Content-带有意图
I found out you can only share either Text or Image, not both using
Intents
. Below code shares only Image if exists, or only Text if Image does not exits. If you want to share both, you need to use Facebook SDK from here.If you use other solution instead of below code, don't forget to specify package name com.facebook.lite as well, which is package name of Facebook Lite. I haven't test but com.facebook.orca is package name of Facebook Messenger if you want to target that too.
You can add more methods for sharing with WhatsApp, Twitter ...
For getting Uri from File, use below class:
For writing FileProvider, use this link: https://github.com/codepath/android_guides/wiki/Sharing-Content-with-Intents
这是我所做的(对于文本)。在代码中,我将所需的任何文本复制到剪贴板。第一次有人尝试使用分享意图按钮时,我会弹出一条通知,解释他们是否希望分享到 Facebook,他们需要单击“Facebook”,然后长按进行粘贴(这是为了让他们知道 Facebook已经破坏了 android 意图系统)。然后相关信息就在字段中。我还可能包含这篇文章的链接,以便用户也可以抱怨...
下面是处理先前版本的方法
Here is what I did (for text). In the code, I copy whatever text is needed to clipboard. The first time an individual tries to use the share intent button, I pop up a notification that explains if they wish to share to facebook, they need to click 'Facebook' and then long press to paste (this is to make them aware that Facebook has BROKEN the android intent system). Then the relevant information is in the field. I might also include a link to this post so users can complain too...
Below is a method for dealing w/prior versions
在 Lollipop (21) 中,您可以使用
Intent.EXTRA_REPLACEMENT_EXTRAS
专门覆盖 Facebook 的意图(并仅指定链接)https://developer.android.com/reference/android/content/Intent.html#EXTRA_REPLACMENT_EXTRAS
In Lollipop (21), you can use
Intent.EXTRA_REPLACEMENT_EXTRAS
to override the intent for Facebook specifically (and specify a link only)https://developer.android.com/reference/android/content/Intent.html#EXTRA_REPLACEMENT_EXTRAS
该解决方案也有效。如果没有安装 Facebook,它只会运行普通的共享对话框。如果存在并且您尚未登录,则会进入登录屏幕。如果您已登录,它将打开共享对话框并输入 Intent Extra 中的“共享 url”。
This solution works aswell. If there is no Facebook installed, it just runs the normal share-dialog. If there is and you are not logged in, it goes to the login screen. If you are logged in, it will open the share dialog and put in the "Share url" from the Intent Extra.
Facebook 4.0.0 版本似乎改变了很多事情。这是我的代码,运行良好。
Seems in version 4.0.0 of Facebook so many things has changed. This is my code which is working fine.
这是我用链接打开 Facebook 应用程序所做的事情
Here is something I did which open Facebook App with Link
Facebook 不允许与
Intent.EXTRA_TEXT
共享纯文本数据,但您可以使用它与 facebook Messenger 共享文本+链接,这对我来说效果很好Facebook does not allow to share plain text data with
Intent.EXTRA_TEXT
but You can share text+link with facebook messanger using this, this works fine for me我能找到的将消息从我的应用程序传递到 Facebook 的最简单方法是以编程方式复制到剪贴板并提醒用户他们可以选择粘贴。它使用户免于手动执行此操作;我的应用程序没有粘贴,但用户可能会粘贴。
The easiest way that I could find to pass a message from my app to facebook was programmatically copy to the clipboard and alert the user that they have the option to paste. It saves the user from manually doing it; my app is not pasting but the user might.