简体   繁体   English

丰富的通知操作不可点击

[英]Rich notification action not clickable

I'm using Jelly Bean's rich notification system and I'm adding two actions to my notification. 我正在使用Jelly Bean的丰富通知系统,并在通知中添加了两个操作。 The problem I have is the actions seem to be disabled. 我的问题是操作似乎被禁用。

The idea is to set a PendingIntent to each one of the actions and get notified in a BroadcastReceiver which I registered to handle the actions in the two intents. 这个想法是为每个动作设置一个PendingIntent ,并在我注册的BroadcastReceiver中得到通知,以处理这两个意图中的动作。

Here is the code for creating the notifications: 这是用于创建通知的代码:

        Intent startIntent = new Intent(Notifications.START);
        Intent resetIntent = new Intent(Notifications.RESET);

        PendingIntent startPendingIntent = PendingIntent.getBroadcast(ctx,
                whichOne, startIntent,
                Intent.FLAG_RECEIVER_REPLACE_PENDING);
        PendingIntent resetPendingIntent = PendingIntent.getBroadcast(ctx,
                whichOne, resetIntent,
                Intent.FLAG_RECEIVER_REPLACE_PENDING);

        [...]

        notificationBuilder = new Notification.Builder(ctx)
                .setContentTitle(s)
                .setContentText("")
                .setContentIntent(appIntent)
                .setSmallIcon(R.drawable.ic_launcher)

                .addAction(R.drawable.play,
                        ctx.getString(R.string.START), startPendingIntent)
                .addAction(R.drawable.reload,
                        ctx.getString(R.string.RESET_TIMER),
                        resetPendingIntent);
        [...]

Then I register the Receiver like this: 然后我像这样注册Receiver

IntentFilter filter = new IntentFilter();
    filter.addAction(Notifications.START);
    filter.addAction(Notifications.RESET);

    receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Toast.makeText(context, intent.getAction(), Toast.LENGTH_LONG)
                    .show();

        }
    };

    registerReceiver(receiver, filter);

The problem I have is the actions seems to be disabled. 我的问题是操作似乎被禁用。 I cannot click them and they are displayed with the typical disabled alpha text color. 我无法单击它们,它们以典型的禁用字母文本颜色显示。

You should remove the Intent.FLAG_RECEIVER_REPLACE_PENDING flag from your PendingIntent for both actions and replace it with 0. That flag is most commonly used with sticky broadcasts where the most recent value is the only one that matters (eg checking battery level). 您应该从PendingIntent删除这两个动作的Intent.FLAG_RECEIVER_REPLACE_PENDING标志,并将其替换为0。 该标志最常用于粘性广播 ,其中最新值是唯一重要的值(例如,检查电池电量)。

Removing that flag will re-enable your actions. 删除该标志将重新启用您的操作。 If your intent was to only make one action available at any given time then I suggest using a static ID for that notification so that only one notification of that type can be used at any given time. 如果您的意图是在任何给定时间仅执行一项操作,那么我建议对该通知使用静态ID,以便在任何给定时间只能使用该类型的一个通知。

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

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