如果我创建一个通知,我可以添加三个操作。每个操作也可以在手表上调用。是否有可能使其中一些操作在Android Wear手表上不可用?
NotificationCompat.WearableExtender
中使用addAction()
时,实际上并没有扩展操作(尽管名称如此),而是将它们分为两个列表,一个用于手机,另一个用于可穿戴设备。
NotificationCompat.Builder
上添加的操作将显示在手持设备上。WearableExtender
上添加的操作将显示在Android Wear设备上。因此,要有仅限手持设备的操作,请在创建扩展程序之前添加它们。要有仅限可穿戴设备的操作,请在扩展程序上添加它们。如果您使用扩展程序并且想在两个设备中重复操作,则必须在两个设备中都添加它们(虽然也许有一种选项可以将它们复制?)。如果您希望可穿戴设备上可用的操作与手持设备上的操作不同,则使用
WearableExtender.addAction()
。一旦您使用此方法添加了一个操作,可穿戴设备将不会显示使用NotificationCompat.Builder.addAction()
添加的任何其他操作。也就是说,只有使用WearableExtender.addAction()
添加的操作才会显示在可穿戴设备上,而它们不会出现在手持设备上。
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("The title")
.setContentText("This is the text")
.setContentIntent(pendingIntent);
// Handheld-only actions.
notificationBuilder.addAction(drawable1, "In Both", pendingIntent);
notificationBuilder.addAction(drawable2, "Only in phone", pendingIntent);
// Wearable-only actions.
NotificationCompat.WearableExtender wearableExtender = new NotificationCompat.WearableExtender();
wearableExtender.addAction(new NotificationCompat.Action.Builder(drawable2, "In Both", pendingIntent).build());
wearableExtender.addAction(new NotificationCompat.Action.Builder(drawable3, "Only in wearable", pendingIntent).build());
notificationBuilder.extend(wearableExtender);
// Build and show notification.
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(notificationId, notificationBuilder.build());
同时
WearableExtender
但是没有添加任何操作,那么原始通知中的操作将被使用。