使用 ShareActionProvider 在外部共享对象列表
Share an object list externally using ShareActionProvider
我有一个 RecyclerView,它需要 sent/shared 与 WhatsApp 等外部应用程序一起使用。使用 ShareActionProvider 来实现此目的。
使用 SharedActionProvider 创建了一个菜单并在 ToolBar 的溢出区域中膨胀。
示例代码。
public void prepareShareIntent() {
shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT, "Sample Text.");
shareIntent.setType("text/plain");
startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.share)));
}
如果内部活动之间共享数据,我们通常通过序列化/Parcelable 或使用 gson 传递 objects/object 列表,这里没问题,因为我们可以轻松地在接收 activity 中解析数据。但以防万一,如果我需要将列表对象共享给外部应用程序,应该怎么做。外部应用程序是否仅解释为特定类型?
欣赏可能的方法。
提前致谢。
I have a RecyclerView which needed to be sent/shared with external apps like WhatsApp etc.
那是不可能的。首先,您不能将 View
传递给另一个进程。其次,WhatsApp 和其他应用程序将不知道如何处理它。
If data sharing between internal activities, we usually pass objects/object list by serialising/ Parcelable or using gson , no issue here since we can easily parse the data in the receiving activity.
您无法将 RecyclerView
转换为 Serializable
、Parcelable
或 JSON。您也许可以将 模型数据 转换为 RecyclerView
使用的其中一种东西,但第三方应用程序(例如 WhatsApp)可能不知道如何处理它。
Does the external apps interpret as only specific types?
正确。 ACTION_SEND
Intent
的 MIME 类型需要是通过 EXTRA_TEXT
或 EXTRA_STREAM
.
共享的内容的 MIME 类型
我建议您花些时间思考 确切地 当您与 WhatsApp 共享内容时,您希望它做什么,而该内容以某种方式与 RecyclerView
.例如,如果您希望 WhatsApp 将 RecyclerView
的屏幕截图发送给某人,那么您应该共享 RecyclerView
的屏幕截图,而不是 RecyclerView
本身。
我有一个 RecyclerView,它需要 sent/shared 与 WhatsApp 等外部应用程序一起使用。使用 ShareActionProvider 来实现此目的。 使用 SharedActionProvider 创建了一个菜单并在 ToolBar 的溢出区域中膨胀。
示例代码。
public void prepareShareIntent() {
shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT, "Sample Text.");
shareIntent.setType("text/plain");
startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.share)));
}
如果内部活动之间共享数据,我们通常通过序列化/Parcelable 或使用 gson 传递 objects/object 列表,这里没问题,因为我们可以轻松地在接收 activity 中解析数据。但以防万一,如果我需要将列表对象共享给外部应用程序,应该怎么做。外部应用程序是否仅解释为特定类型?
欣赏可能的方法。 提前致谢。
I have a RecyclerView which needed to be sent/shared with external apps like WhatsApp etc.
那是不可能的。首先,您不能将 View
传递给另一个进程。其次,WhatsApp 和其他应用程序将不知道如何处理它。
If data sharing between internal activities, we usually pass objects/object list by serialising/ Parcelable or using gson , no issue here since we can easily parse the data in the receiving activity.
您无法将 RecyclerView
转换为 Serializable
、Parcelable
或 JSON。您也许可以将 模型数据 转换为 RecyclerView
使用的其中一种东西,但第三方应用程序(例如 WhatsApp)可能不知道如何处理它。
Does the external apps interpret as only specific types?
正确。 ACTION_SEND
Intent
的 MIME 类型需要是通过 EXTRA_TEXT
或 EXTRA_STREAM
.
我建议您花些时间思考 确切地 当您与 WhatsApp 共享内容时,您希望它做什么,而该内容以某种方式与 RecyclerView
.例如,如果您希望 WhatsApp 将 RecyclerView
的屏幕截图发送给某人,那么您应该共享 RecyclerView
的屏幕截图,而不是 RecyclerView
本身。