android - 如何创建 Notification.Action 并将其添加到自定义通知

标签 android android-notifications android-support-library wear-os

我在此处遵循 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/

相关文章:

java - 更改 android.R.layout.simple_list_item_2 中的文本颜色

android - 我如何知道在通知中点击了哪个按钮?

java - Android:通知栏中未显示通知

android - 在 Lollipop 崩溃前使用 android vector Drawables

android - 做动画的时候怎么把view放在前面?

android - 预加载 CursorAdapter View

使用android的sip进行安卓视频通话

android - 单击关闭按钮后如何关闭通知对话框,而不调用任何 Activity

Android AnimatedVectorDrawable - 属性 : is not supported for FullPath

android - Eclipse 中 Android 项目的 Javadoc 因 NullPointerException 而失败