Android,播放parcelable数据

时间:2014-01-05 01:53:31

标签: android android-intent android-notifications android-broadcast

我已经实现了一个扩展NotificationListenerService的类,它可以很好地接收发布的通知。

然后我想要接收statusBarNotification对象并广播它。

我会做以下事情:

@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

4 个答案:

答案 0 :(得分:1)

我的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,此解决方案可能会破坏通知应用的功能(应用可能会认为其他内容未经检查)。

答案 1 :(得分:0)

这可能是某些用户错误,正如一些用户在上面提到的那样。如果你想绕过它并仍然使用尽可能多的bundle考虑实现自定义bundle serializer / deserializer。我已回答了如何在How to serialize a Bundle?中构建此类内容的问题 缺少的是如何在打包/打包包裹时实际使用它。这在这里描述:

@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
    }

答案 2 :(得分:0)

我只碰撞了kitkat(api 19)。 Муerror(显示所有错误scrooling到右):

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

答案 3 :(得分: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);

}