【发布时间】:2021-09-22 10:25:39
【问题描述】:
我有两个安卓设备。我开发了两个代码库。一个是发送者代码库,另一个是接收者。这两个都是通过蓝牙套接字连接的,我可以在它们之间发送简单的字符串。
在发送方设备上,我实现了一个NotificationListenerService,它从该设备捕获通知。对于每个通知,我都会得到一个 StatusBarNotification 类型的对象。根据文档,此对象实现 Parcelable 接口。
为了传输这个对象,我尝试过使用 GSON,以及使用 Parcel 对象来编组和发送数据。这不起作用,所以我不再担心发送部分,并尝试在同一设备上查看这是否会正确编组和解组。
这是两者的 Kotlin 代码,
/*
* Using Gson for serialization.
*
* Parts of the object e.g. deserializedObject.notification are missing
*/
val serializedString = Gson().toJson(sbn)
val deserializedObject = Gson().fromJson<StatusBarNotification>(serializedString, StatusBarNotification::class.java)
/*
* Using Parcel for serialization.
*
* Throws this error,
* Caused by: java.lang.RuntimeException: Tried to marshall a Parcel that contained Binder objects.
*/
val parcel = Parcel.obtain()
parcel.writeValue(sbn)
val bytes = parcel.marshall()
val p2 = Parcel.obtain()
p2.unmarshall(bytes, 0, bytes.size)
val result = p2.readValue(StatusBarNotification::class.java.classLoader) as StatusBarNotification
任何方向都将不胜感激。
更新 1
我已尝试创建自定义数据类对象。序列化和反序列化在蓝牙上工作得非常好,我正在第二台设备上获取数据。但是我已经缩小了范围,我的问题仍然是 StatusBarNotification 对象不能很好地进行序列化。
val serializedCode = Gson().toJson(statusBarNotification)
在调试器中给出
{"id":1815187780,"pkg":"org.telegram.messenger","uid":10260}
任何线索将不胜感激。
更新 2
如果有任何通过 Android 实现通知回复的官方方式,也将不胜感激。
【问题讨论】:
-
我一头雾水,你想实现对来自其他设备的通知的回复或正确的序列化/反序列化?
-
我想回复其他设备的通知。序列化/反序列化是该过程的必要部分。
-
这个问题好像太宽泛了?你的代码库中是否有 sn-ps 说明它实际失败的地方,或者你迷失了如何继续?
-
就像我在问题中提到的那样,我无法序列化通知对象。这就是它失败的地方。
标签: android serialization android-notifications android-bluetooth parcelable