问题描述
我实现了扩展NotificationListenerService这对于张贴通知,拿起工作正常类。
I've implemented a class that extends NotificationListenerService which works fine for picking up on notifications posted.
我则希望把收到的statusBarNotification对象,并播放它。
I'm then wanting to take the statusBarNotification object received and broadcast it.
我倒是做了以下内容:
@Override
public void onNotificationPosted(StatusBarNotification statusBarNotification) {
Intent intent = new Intent();
intent.putExtra("STATUS_BAR_NOTIFICATION",statusBarNotification);
intent.setAction("com.example.NotificationPosted");
sendBroadcast(intent);
}
但是,当我这样做,我得到了以下错误:
But when I do this I get the following error:
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
Can anyone see what I'm doing wrong, or is this not possible. StatusBarNotification implements Parcelable
推荐答案
我有同样的问题,从Twitter通知。我成功地解决了该通知的额外设置为null。
I've had the same problem with notifications from Twitter.I successfully solved it setting the notification's extras to 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(应用程序能想到的演员在那里,不检查)。
Please note that this solution could break the functionality of notification's app if you send the contentIntent (the app could think the extras are there without checking).
这篇关于Android的广播parcelable数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!