String in Message 的优雅解决方案

Elegant solution for String in Message

我正在编写一个 Android 服务,旨在受其他应用程序的约束。它使用 Messenger 作为 IBinder

现在我遇到了一个问题:如果我想发送一个 Message 只有一个 what 和一个 String 我原本计划使用 Message.obj为了它。 正如文档所述,这不起作用:

When using Messenger to send the message across processes this can only be non-null if it contains a Parcelable of a framework class (not one implemented by the application). For other data transfer use setData(Bundle).

这提出了两个(相关)问题:

  1. 为什么 String 不是 Android 中的 Parcelable
  2. 有没有比为它创建一个 Bundle 并在那里设置我的 String 更 "elegant" 的解决方案?

1)因为它与 Java 的字符串 class 兼容,它不是 Parcelable(因为 java 标准库中不存在 2)因为它通常不需要 - 字符串可以在大多数情况下通过本地网络发送而无需打包。你刚刚发现了一个奇怪的角落案例。

不得不说,在活页夹上使用消息有点奇怪。通常,您只需将数据作为单独的参数发送给调用。