0

I am trying to send an array of StatusBarNotifications to another service of mine so I have tried this:

Service that extends 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);
}

Service to receive array through 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
}

Edit

However I get a ClassCastException(marked above with comment)

Here is the log:

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[]

I read quite a bit about sending objects through intents and about implementing Parcelable, although it was pretty complicated, see here for example. But from what I can see StatusBarNotification (see API doc here) already implements Parcelable, thus I should be able to send it through an intent with all of its content, right?

Would really appreciate some help here as I am stuck, thanks

REG1
  • 486
  • 4
  • 15

2 Answers2

2

Instead

activeNotifications = (StatusBarNotification[]) intent.getExtras().get("activenotifications");

call

Parcelable[] parcelable = intent.getParcelableArrayExtra("activenotifications");
StatusBarNotification[] sbn = Arrays.copy(parcelable, parcelable.lenght, StatusBarNotification[].class);
Damian Petla
  • 8,963
  • 5
  • 44
  • 47
  • Thanks for the suggestion, but I still get a NullPointerException at the same point. Any idea why that could be? – REG1 Aug 22 '14 at 18:56
  • I realized that I had spelt the tag incorrectly (they didn't match), that was a stupid mistake. However, I still get an Exception (ClassCastException) when I try to receive the intent (see my code). Would appreciate it if you could look at it again, thanks again – REG1 Aug 22 '14 at 22:24
  • 1
    I was rushing a bit with my answer. Of course this casting would not work. You already got it right from others but I have updated my answer to be complete and useful for other SO visitors. – Damian Petla Aug 23 '14 at 06:47
1

For the ClassCastException, see: I don't get why this ClassCastException occurs

Basically you take the Parcelable array, iterate over it, and copy the items into a StatusBarNotification array by casting the items while iterating.

Community
  • 1
  • 1
Daniel Zolnai
  • 16,487
  • 7
  • 59
  • 71
  • Thanks, for the answer, I used the Arrays.copyOf() method which seems to do the whole process for me (suggested by Brodo Fraggins) – REG1 Aug 22 '14 at 23:05