我在此处遵循 Google 的说明 https://developer.android.com/training/wearables/notifications/creating.html
然而,毫不奇怪,他们的代码不起作用。具体来说,我正在尝试这样做:
// Build the notification and add the action via WearableExtender
Notification notification =
new Notification.Builder(context)
.setSmallIcon(R.drawable.buzz_icon)
.setContentTitle("Buzz")
.setContentText("OpenBuzz")
.extend(new Notification.WearableExtender().addAction(action))
.build();
我想要特定于可穿戴设备的操作,所以我别无选择,只能使用 Notification.WearableExtender()
。但它的 addAction 方法只接受一个 Action 作为它的参数。这是我创建 Action 的代码:
Notification.Action action =
Notification.Action.Builder(R.drawable.buzz_icon, actionIntent.toString(), pendingIntent);
这是行不通的,因为 Android Studio 说“预期方法调用”
如何成功创建 Notification.Action
?
或者我还可以如何将可穿戴设备特定操作添加到我的通知中?
最佳答案
你走在正确的轨道上。
您需要创建一个新的NotificationCompat.Action.Builder
,然后对其调用build()
.像这样:
NotificationCompat.Action action =
new NotificationCompat.Action.Builder(android.R.drawable.ic_menu_call, "Only in wearable", pendingIntent)
.build();
此外,请确保操作定义为 NotificationCompat.Action
,而不是 Notification.Action
。
关于android - 如何创建 Notification.Action 并将其添加到自定义通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24685318/