果冻豆通知操作已禁用

时间:2013-01-13 17:27:32

标签: android broadcastreceiver action android-4.2-jelly-bean

我正在构建一个应用程序,当调用BroadcastReceiver时,该应用程序显示带有两个选项“Dim”和“Full”的通知。每个按钮都会广播一个动作。

到目前为止,一切皆有可能,对吧?

问题是通知上显示的按钮不会响应点按,但整个通知都会响应(如果我点击图标或文字而不是按钮)。

我有这个函数来构建通知:

private Notification buildReleaseNotification(NotificationManager nManager, Context context) {

    Notification.Builder builder = new Builder(context);
    builder.addAction(R.drawable.rate_star_big_half_holo_dark, "Dim", buildPendingIntent(context, DIM));
    builder.addAction(R.drawable.rate_star_big_on_holo_dark, "Full", buildPendingIntent(context, FULL));

    builder.setContentTitle("Car notification");
    builder.setContentText("Freed");
    builder.setSmallIcon(R.drawable.ic_launcher);
    builder.setOngoing(true);

    Notification notification = builder.build();
    return notification;
}

并在接收广播时调用:

    public void onReceive(Context context, Intent intent) {

    NotificationManager nManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    Notification noty = null;
    noty = buildReleaseNotification(context);
    startService(context, RELEASE);

    if (noty != null) {
        nManager.notify(ChangeLockService.GLOBAL_TAG, ID, noty);
    }

}

------编辑

注意到以下函数返回null ...那么,我如何构建一个挂起的意图来执行广播呢?

private PendingIntent buildPendingIntent(Context context, String action) {

        Intent i = new Intent(action);
        PendingIntent pi = PendingIntent.getBroadcast(context, ID, i, Intent.FLAG_RECEIVER_REPLACE_PENDING);
        return pi;
    }

1 个答案:

答案 0 :(得分:1)

通过

创建意图时
PendingIntent pi = PendingIntent.getBroadcast(context, ID, i, Intent.FLAG_RECEIVER_REPLACE_PENDING);

导致它返回null。 根据文档,我理解标志Intent.FLAG_RECEIVER_REPLACE_PENDING将替换已存在的该按钮的任何待定意图。如果我没有发送标志,那么一切正常:

PendingIntent pi = PendingIntent.getBroadcast(context, ID, i, 0);