【问题标题】:Android Wear - Notification - setContentAction() not workingAndroid Wear - 通知 - setContentAction() 不起作用
【发布时间】:2014-07-30 00:44:11
【问题描述】:

我正在创建一个从可穿戴设备触发的通知,并且只在可穿戴设备上触发,而不是在手机上。我希望它有两个操作按钮(尚无功能)和单击通知本身时的第三个操作。我正在尝试使用 setContentAction() 使最后一个操作成为单击通知时的操作,但它仍显示为单独的操作按钮(根据文档here,它不应该显示单独的按钮)。不过,那个不需要的按钮会触发所需的意图。通知本身没有响应点击。以下是创建通知的代码:

    Intent pictureIntent = new Intent(this, PictureActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 254, pictureIntent, PendingIntent.FLAG_ONE_SHOT);

    NotificationCompat.Builder notificationBuilder =
            new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.medicinepillmd)
                    .setContentTitle(dose[0])
                    .setContentText(dose[3])
                    .extend(new NotificationCompat.WearableExtender()
                            .setContentIcon(R.drawable.thumbnail)
                            .setBackground(BitmapFactory.decodeResource(getResources(), R.drawable.orangegirl))
                            .addAction(new NotificationCompat.Action.Builder(R.drawable.medicinepillmd, "Taken", null).build())
                            .addAction(new NotificationCompat.Action.Builder(R.drawable.thumbnail, "Skipped", null).build())
                            .addAction(new NotificationCompat.Action.Builder(0, null, pendingIntent).build())
                            .setContentAction(2));

有人知道为什么这可能没有按预期运行吗?任何输入表示赞赏。谢谢

【问题讨论】:

    标签: android android-notifications wear-os


    【解决方案1】:

    原因:

    setContentAction(int index) 允许您指定将“合并”到卡片中的操作。它确实从“操作页面”中删除了此操作,并且它只会出现在您的主卡上。但是,问题的原因是您没有为Action 指定图标资源。没有图标不能合并到主卡。

    解决方案:

    您需要指定一些图标并将其添加到您的操作中:

    .addAction(new NotificationCompat.Action.Builder(R.drawable.some_icon, null, pendingIntent).build())
    

    如果你真的不想要任何图标,你可以使用带有空图标的“hack”:

    empty_icon.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" >
    </shape>
    


    注意setContentIntent(PendingIntent intent) 不会让您的卡片可点击。它只会创建并添加另一个带有标签的动作(在最右边):“打开”。

    【讨论】:

    • 这个答案解决了我的问题。显然你必须指定一个图标,就像你说的那样,即使它是空的。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多