Android Wear - Notification - setContentAction() not working

試著忘記壹切 提交于 2019-12-01 03:34:02

问题


I'm creating a notification that fires from the wearable device and only on the wearable device, not on phone at all. I want it to have two action buttons (no functionality yet) and a third action when the notification itself is clicked. I'm trying to use setContentAction() to make the last action be the action when the notification is clicked, but it's still displaying as a separate action button (according to the documentation here it shouldn't display a separate button). That unwanted button fires the desired intent, though. The notification itself isn't responding to clicks. Here is the code to create the notification:

    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));

Anyone know why this might not be behaving as intended? Any input appreciated. Thanks


回答1:


Reason:

setContentAction(int index) allow you to specify an action that will be "merged" into the card. It DOES remove this action from "action pages" and it will only be present at your main card. However the reason of your problem is that you do not specify the icon resource for your Action. Without icon it cannot be merged to the main card.

Solution:

You need to specify some icon and add it to your action:

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

If you really do not want any icon you can use a "hack" with empty icon:

empty_icon.xml:

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


NOTE: setContentIntent(PendingIntent intent) will not make your card clickable. It will just create and add another action (to the most right) with label: "Open".



来源:https://stackoverflow.com/questions/25018086/android-wear-notification-setcontentaction-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!