繁体   English   中英

如何指定Android Wear上未显示通知操作?

[英]How can I specify that an Notification action is not shown on Android Wear?

如果我创建通知,我可以添加三个操作。 每个动作也可以在手表上调用。 是否有可能在Android Wear Watch上无法使用此操作?

无论何时在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但不向其中添加任何操作,则使用原始通知中的操作。
  • 手持设备的“内容意图”似乎总是出现在手表上,带有“打开电话”文本。 我还没有找到一种方法只为手表禁用此功能。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM