Android,广播Parcelable数据

9
我已经实现了一个扩展NotificationListenerService的类,用于接收发布的通知,目前运行正常。
我想使用收到的statusBarNotification对象进行广播。
具体操作如下: ```java ```
@Override
public void onNotificationPosted(StatusBarNotification statusBarNotification) {

    Intent intent = new Intent();
    intent.putExtra("STATUS_BAR_NOTIFICATION",statusBarNotification);
    intent.setAction("com.example.NotificationPosted");
    sendBroadcast(intent);
}

但是当我这样做时,会出现以下错误:
01-05 01:50:14.333  19574-19673/com.example W/NotificationListenerService[NotificationListener]﹕ Error running onNotificationPosted
java.lang.RuntimeException: Not allowed to write file descriptors here
        at android.os.Parcel.nativeAppendFrom(Native Method)
        at android.os.Parcel.appendFrom(Parcel.java:431)
        at android.os.Bundle.writeToParcel(Bundle.java:1679)
        at android.os.Parcel.writeBundle(Parcel.java:636)
        at android.app.Notification.writeToParcel(Notification.java:962)
        at android.service.notification.StatusBarNotification.writeToParcel(StatusBarNotification.java:106)
        at android.os.Parcel.writeParcelable(Parcel.java:1285)
        at android.os.Parcel.writeValue(Parcel.java:1204)
        at android.os.Parcel.writeArrayMapInternal(Parcel.java:618)
        at android.os.Bundle.writeToParcel(Bundle.java:1692)
        at android.os.Parcel.writeBundle(Parcel.java:636)
        at android.content.Intent.writeToParcel(Intent.java:7013)
        at android.app.ActivityManagerProxy.broadcastIntent(ActivityManagerNative.java:2361)
        at android.app.ContextImpl.sendBroadcast(ContextImpl.java:1127)
        at android.content.ContextWrapper.sendBroadcast(ContextWrapper.java:365)
        at com.example.NotificationListener.onNotificationPosted(NotificationListener.java:113)
        at android.service.notification.NotificationListenerService$INotificationListenerWrapper.onNotificationPosted(NotificationListenerService.java:168)
        at android.service.notification.INotificationListener$Stub.onTransact(INotificationListener.java:56)
        at android.os.Binder.execTransact(Binder.java:404)
        at dalvik.system.NativeStart.run(Native Method)

有人能看出我做错了什么吗,或者这不可能吗。StatusBarNotification实现了Parcelable接口。


你看过这个评论吗?虽然它不是真正的答案,但他可能有眉目。 - A--C
1
经过一些实验,有些通知可以正常工作,而有些则不行。例如Google Keep通知和其他一些通知可以正常工作,但Facebook Messenger等应用则不行。由于它是一个可包含的Android类,我只能认为这是Android系统中的一个错误。 - Andrew
4个回答

2

我曾经遇到过来自 Twitter 的通知问题。

成功解决的方法是将通知的额外参数设置为 null。

你可以尝试这样做:

@Override
@SuppressLint("NewApi") // Notification.extras is only available in API Level 19+
public void onNotificationPosted(StatusBarNotification statusBarNotification) {

    // Apprarently, the bug is caused by the extras when they're written to the parcel
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
         statusBarNotification.getNotification().extras = null;
    }
    Intent intent = new Intent();
    intent.putExtra("STATUS_BAR_NOTIFICATION",statusBarNotification);
    intent.setAction("com.example.NotificationPosted");
    sendBroadcast(intent);
}

请注意,如果您发送contentIntent(应用程序可能认为extras已经存在而没有检查),此解决方案可能会破坏通知应用程序的功能。

在KitKat中,将extras设置为null将使其失去任何好处。拥有Bundle会更加方便... - Mgamerz

0

这个解决方案对我有效:

@Override
public void onNotificationPosted(StatusBarNotification statusBarNotification) {

Bundle bundle = new Bundle(); statusBarNotification.getNotification().extras.putBundle("android.car.EXTENSIONS", bundle);

Intent intent = new Intent();
intent.putExtra("STATUS_BAR_NOTIFICATION",statusBarNotification);
intent.setAction("com.example.NotificationPosted");
sendBroadcast(intent);

}


0

这可能是一些 Android 的 bug,正如一些用户在上面提到的。如果你想绕过它并仍然尽可能多地使用 bundle,请考虑实现自定义 bundle 序列化程序/反序列化程序。我已经回答了一个问题,关于如何构建这样的东西,在 如何序列化 Bundle? 中。缺少的是在打包/解包 parcel 时如何实际使用它。这在这里描述:

@Override public void writeToParcel(Parcel dest, int flags) {
    byte[] bytes = serializeBundle(yourBundle);
    dest.writeInt(bytes.length);
    dest.writeByteArray(bytes);
}

然后

    YourConstructor(Parcel in) {
         byte[] bytes = new byte[in.readInt()]; // read the length of the array
         in.readByteArray(bytes); // read bytes to array 
         yourBundle = deserializeBundle(bytes); // unpack the bundle
    }

0

我只在KitKat(API 19)上崩溃。我的错误信息(将所有错误向右滚动):

Error running onNotificationPosted
                                                                                                                    java.lang.AbstractMethodError: abstract method not implemented
                                                                                                                          at android.service.notification.NotificationListenerService.onNotificationPosted(NotificationListenerService.java)
                                                                                                                          at service.CustomNotificationListenerService.onNotificationPosted(CustomNotificationListenerService.kt:46)
                                                                                                                          at android.service.notification.NotificationListenerService$INotificationListenerWrapper.onNotificationPosted(NotificationListenerService.java:168)
                                                                                                                          at android.service.notification.INotificationListener$Stub.onTransact(INotificationListener.java:56)
                                                                                                                          at android.os.Binder.execTransact(Binder.java:404)                                                                                                                     at dalvik.system.NativeStart.run(Native Method)

简单解决方案:我移除了方法:super()


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接