【问题标题】:Android , broadcasting parcelable dataAndroid , 广播可打包数据
【发布时间】:2014-01-05 01:53:30
【问题描述】:

我已经实现了一个扩展 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

【问题讨论】:

  • 你看到this的评论了吗?当然,这不是一个实际的答案,但他可能正在做某事。
  • 经过多一点试验,一些通知可以工作,而另一些则不能。谷歌保留通知和其他一些,但 Facebook Messenger 之类的东西没有。由于它是一个可打包的 Android 类,我只能认为它是 Android 中的一个错误。

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


【解决方案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,此解决方案可能会破坏通知应用程序的功能(应用程序可能认为额外内容存在而不检查)。

【讨论】:

  • 在 kitkat 中,将 extras 设置为 null 将取消它所拥有的所有好处。有一个捆绑包很方便......
【解决方案2】:

这可能是某些用户在上面提到的某种 android 错误。如果您想绕过它并仍然尽可能多地使用捆绑包,请考虑实现自定义捆绑包序列化器/解串器。我已经回答了一个问题如何在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
    }

【讨论】:

    【解决方案3】:

    我只有崩溃 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()

    【讨论】:

      【解决方案4】:

      这个解决方案对我有用:

      @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);
      

      }

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-03-06
        • 1970-01-01
        • 1970-01-01
        • 2014-03-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多