Iam trying to display a notification on a Android Wear watch only. I don't want the notification to be shown on the phone (It has the app open on their), and I don't want it to be in a Activity directly (However, one of the actions should open a Activity).
To accomplish what I want I created a WearableListenerService with a onDataChanged. Within this method I try to create the notification. This is the code I use: @Override public void onDataChanged(DataEventBuffer dataEvents) { Log.d("Listener", "dataChanged");
Intent actionIntent = new Intent(getBaseContext(), WatchMainActivity.class);
actionIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent actionPendingIntent =
PendingIntent.getActivity(getBaseContext(), 0, actionIntent,
PendingIntent.FLAG_CANCEL_CURRENT);
NotificationManagerCompat manager = NotificationManagerCompat.from(getApplicationContext());
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext());
Log.d("listener", "Created things");
builder.setContentTitle("Wearable test")
.setContentText("Testing 123")
.setOngoing(true)
.setSmallIcon(R.drawable.ic_launcher)
;
// Create the action
NotificationCompat.Action action =
new NotificationCompat.Action.Builder(R.drawable.ic_full_sad,
getString(R.string.label), actionPendingIntent)
.build();
NotificationCompat.WearableExtender extender = new NotificationCompat.WearableExtender();
extender.addAction(action)
.setContentAction(0)
.setHintHideIcon(true)
;
Log.d("listener", "action created, extending...");
builder.extend(extender);
Log.d("listener", "extended");
manager.cancel(101);
Log.d("listener", "canceled");
manager.notify(101, builder.build());
Log.d("listener", "notified");
Log.d("listener", "created");
}
And when looking at the log it results in this crash:
D/listener( 1075): listener created
D/Listener( 1075): dataChanged
D/listener( 1075): Created things
D/listener( 1075): action created, extending...
D/listener( 1075): extended
D/listener( 1075): canceled
W/dalvikvm( 597): threadid=20: thread exiting with uncaught exception (group=0xada2fd70)
E/AndroidRuntime( 597): FATAL EXCEPTION: NotificationCollectorService
E/AndroidRuntime( 597): Process: com.google.android.wearable.app, PID: 597
E/AndroidRuntime( 597): java.lang.ClassCastException: android.os.Bundle cannot be cast to android.app.Notification$Action
E/AndroidRuntime( 597): at android.support.v4.app.NotificationCompatApi20.getActionsFromParcelableArrayList(NotificationCompatApi20.java:145)
E/AndroidRuntime( 597): at android.support.v4.app.NotificationCompat$NotificationCompatImplApi20.getActionsFromParcelableArrayList(NotificationCompat.java:532)
E/AndroidRuntime( 597): at android.support.v4.app.NotificationCompat$WearableExtender.<init>(NotificationCompat.java:1876)
E/AndroidRuntime( 597): at com.google.android.clockwork.stream.StreamManager.stripRemoteViewsFromNotification(StreamManager.java:481)
E/AndroidRuntime( 597): at com.google.android.clockwork.stream.StreamManager.maybeStripRemoteViewsFromNotification(StreamManager.java:473)
E/AndroidRuntime( 597): at com.google.android.clockwork.stream.StreamManager.setItem(StreamManager.java:347)
E/AndroidRuntime( 597): at com.google.android.clockwork.stream.StreamManager.onNotificationPosted(StreamManager.java:253)
E/AndroidRuntime( 597): at com.google.android.clockwork.stream.NotificationCollectorService$ServiceHandler.handleMessage(NotificationCollectorService.java:264)
E/AndroidRuntime( 597): at android.os.Handler.dispatchMessage(Handler.java:102)
E/AndroidRuntime( 597): at android.os.Looper.loop(Looper.java:136)
E/AndroidRuntime( 597): at android.os.HandlerThread.run(HandlerThread.java:61)
D/listener( 1075): notified
D/listener( 1075): created
So there is something within the Notification that causes this crash. When I removed the extend call in the builder, it works fine, however the action is of course not added. Due to this, I assume I do something wrong within my Intent/PendingIntent or within the WearableExtender, but I have no idea what I do wrong. When I take the examples from the wear documentation, it results in the same crash.
Someone has any idea on how to solve this crash?