我正在尝试仅在 Android Wear 手表上显示通知。我不希望通知显示在手机上(它在其上打开了应用程序),并且我不希望它直接在活动中(但是,其中一个操作应该打开一个活动)。
为了完成我想要的,我创建了一个带有 onDataChanged 的 WearableListenerService。在此方法中,我尝试创建通知。这是我使用的代码:@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");
}
在查看日志时会导致此崩溃:
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
因此,通知中存在导致此崩溃的某些内容。当我在构建器中删除扩展调用时,它工作正常,但是当然没有添加该操作。因此,我假设我在 Intent/PendingIntent 或 WearableExtender 中做错了什么,但我不知道我做错了什么。当我从磨损文档中获取示例时,它会导致同样的崩溃。
有人对如何解决此崩溃有任何想法吗?