2013-02-09 82 views
1

我正在使用Jelly Bean丰富的通知系统,并且在通知中添加了两项操作。我遇到的问题是行为似乎被禁用。丰富的通知操作无法点击

这个想法是为每个动作设置一个PendingIntent,并在我注册的BroadcastReceiver中获得通知,以处理两个意图中的动作。

以下是创建通知代码:

 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); 
     [...] 

然后我注册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); 

我的问题是行动似乎被禁用。我无法点击它们,它们以典型的禁用alpha文本颜色显示。

回答

0

您应该从PendingIntent中删除Intent.FLAG_RECEIVER_REPLACE_PENDING这两个操作标志,并将其替换为0. That flag is most commonly used with sticky broadcasts其中最新值是唯一重要的值(例如检查电池电量)。

删除该标志将重新启用您的操作。如果您的意图是在任何给定的时间只提供一个动作,那么我建议为该通知使用一个静态ID,以便在任何给定时间只能使用该类型的一个通知。