【发布时间】:2014-08-22 15:59:17
【问题描述】:
我正在尝试将一组 StatusBarNotifications 发送到我的另一个服务,所以我尝试了这个:
扩展NotificationListenerService的服务:
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
// TODO Auto-generated method stub
StatusBarNotification[] activeN = super.getActiveNotifications();
Intent i = new Intent("com.project.now.CORETWO");
i.putExtra("activenotfications", activeN);
startService(i);
}
服务通过intent接收数组:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
Log.i("abc", "onStartCommand");
activeNotifications = (StatusBarNotification[]) intent.getParcelableArrayExtra("activenotifications"); //This line throws exception
}
编辑
但是我收到了ClassCastException(上面标有评论)
这是日志:
08-22 22:49:22.319: E/AndroidRuntime(20801): FATAL EXCEPTION: main
08-22 22:49:22.319: E/AndroidRuntime(20801): Process: com.project.now, PID: 20801
08-22 22:49:22.319: E/AndroidRuntime(20801): java.lang.RuntimeException: Unable to start service com.project.now.CoreTwo@425ed8d0 with Intent { act=com.project.now.CORETWO (has extras) }: java.lang.ClassCastException: android.os.Parcelable[] cannot be cast to android.service.notification.StatusBarNotification[]
我读过很多关于通过意图发送对象和implementing Parcelable 的文章,虽然它相当复杂,例如参见here。但据我所知,StatusBarNotification(参见 API 文档here)已经实现了 Parcelable,因此我应该能够通过 Intent 发送它的所有内容,对吧?
在我遇到困难时非常感谢您的帮助,谢谢
【问题讨论】:
-
@BrodoFraggins 感谢您的链接,它为我做了:)
标签: android arrays android-intent parcelable