gpt4 book ai didi

android - 通过 facebook 为 android jelly bean 分享 Intent

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:07:03 26 4
gpt4 key购买 nike

我想通过 Facebook 从 android jelly bean 分享一些图像和文本。它适用于除 android jelly bean 之外的所有设备。任何人请帮助我如何解决这个问题。

我的代码

  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);

System.out.println("package name"+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;

在这段代码中,我想输入一些文本。但是 EXTRA_TEXT 不起作用。如何在此 EXTRA_TEXT 中传递字符串或图像。

最佳答案

您不应该只直接定位 Facebook。

相反,您应该只使用 Share Intent。按照 SDK 中指定的方式在 extras 中传递您的数据,然后让用户选择他们想要共享的对象。不要强制他们使用 Facebook,因为它们不是唯一的社交媒体。还有 twitter、4square 和用户使用的许多其他网站。让这些应用处理您的 Intent ,不要规定如此严格的限制,因为这不是 Intent 操作 ACTION_SEND 的工作方式。

至于您的 Extra 问题,显然 EXTRA_TEXT Intent 将不起作用,因为它不用于图像或任何类似的引用。

阅读文档

http://developer.android.com/reference/android/content/Intent.html#ACTION_SEND

它说:

When launching a SEND intent, you should usually wrap it in a chooser (through createChooser(Intent, CharSequence)), which will give the proper interface for the user to pick how to send your data and allow you to specify a prompt indicating what they are doing.

Input: getType() is the MIME type of the data being sent. get*Extra can have either a EXTRA_TEXT or EXTRA_STREAM field, containing the data to be sent. If using EXTRA_TEXT, the MIME type should be "text/plain"; otherwise it should be the MIME type of the data in EXTRA_STREAM. Use / if the MIME type is unknown (this will only allow senders that can handle generic data streams). If using EXTRA_TEXT, you can also optionally supply EXTRA_HTML_TEXT for clients to retrieve your text with HTML formatting.

As of JELLY_BEAN, the data being sent can be supplied through setClipData(ClipData). This allows you to use FLAG_GRANT_READ_URI_PERMISSION when sharing content: URIs and other advanced features of ClipData. If using this approach, you still must supply the same data through the EXTRA_TEXT or EXTRA_STREAM fields described below for compatibility with old applications. If you don't set a ClipData, it will be copied there for you when calling startActivity(Intent).

Optional standard extras, which may be interpreted by some recipients as appropriate, are: EXTRA_EMAIL, EXTRA_CC, EXTRA_BCC, EXTRA_SUBJECT.

鉴于此信息,您还需要在您的 Intent 类型中为您的特定数据设置 mimetype。

关于android - 通过 facebook 为 android jelly bean 分享 Intent ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15025156/

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