gpt4 book ai didi

android - 果冻 bean 通知操作已禁用

转载 作者:行者123 更新时间:2023-11-29 01:57:40 28 4
gpt4 key购买 nike

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

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

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

我有这个功能来构建通知:

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...那么,我如何构建一个待处理的 Intent 来执行广播?

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

最佳答案

创建 Intent 时通过

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

导致它返回 null。根据文档,我了解到标志 Intent.FLAG_RECEIVER_REPLACE_PENDING 将替换该按钮已经存在的任何未决 Intent 。如果我不发送标志,那么一切正常:

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

关于android - 果冻 bean 通知操作已禁用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14306136/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com