- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经面临这个问题很长时间了,我有一个具有很多功能的应用程序,其中之一就是闹钟
我的通知只是停留在那里,永远不会消失尽管我正在打电话cancelAll()
/cancel()
从通知管理器,我也有 autoCancel
设置为true
!它还保持可点击状态,我只需要它在触发操作后消失!
(我正在使用 Android Studio 的模拟器上进行测试,不确定这是否是问题所在)
我搜索了很多,尝试了很多东西,但没有任何效果,所以我很感激您的帮助:)
我已将通知设置如下:
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// ...
private void initNotification() {
setNotificationChannel();
notificationBuilder = new NotificationCompat.Builder(getApplicationContext(), notificationChannel.getId());
Intent click_intent = new Intent(getApplicationContext(), RingtonePlayingService.class)
.putExtra("intent_action", "click")
.putExtra("REQUEST_CODE", alarmRequestCode)
.putExtra("ID", alarmId);
PendingIntent click_pending = PendingIntent.getService(this, MainActivity.getRequestCode(), click_intent, PendingIntent.FLAG_UPDATE_CURRENT);
Intent dismiss_intent = new Intent(getApplicationContext(), RingtonePlayingService.class)
.putExtra("intent_action", "dismiss")
.putExtra("REQUEST_CODE", alarmRequestCode)
.putExtra("ID", alarmId);
PendingIntent dismiss_pending = PendingIntent.getService(getApplicationContext(),MainActivity.getRequestCode(), dismiss_intent, PendingIntent.FLAG_UPDATE_CURRENT);
Intent snooze_intent = new Intent(getApplicationContext(), RingtonePlayingService.class)
.putExtra("intent_action", "snooze")
.putExtra("REQUEST_CODE", alarmRequestCode)
.putExtra("ID", alarmId);
PendingIntent snooze_pending = PendingIntent.getService(getApplicationContext(),MainActivity.getRequestCode(), snooze_intent, PendingIntent.FLAG_UPDATE_CURRENT);
dismissAction = new NotificationCompat.Action(R.drawable.small_delete,
"Dismiss", dismiss_pending);
snoozeAction = new NotificationCompat.Action(R.drawable.bell_ring,
"Snooze", snooze_pending);
notificationBuilder.setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE)
.setSmallIcon(R.drawable.alarm)
.setContentTitle("Alarm On!")
.setContentText("Click the notification to dismiss")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(click_pending)
.setDeleteIntent(click_pending)
.addAction(dismissAction)
.addAction(snoozeAction)
.setAutoCancel(true);
}
private void showNotification() {
notificationManager.notify(alarmRequestCode, notificationBuilder.build());
}
private void setNotificationChannel() {
channelId = "alarm_channel_id";
channelName = "alarm_notification_channel";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
notificationChannel = new NotificationChannel(channelId, channelName, importance);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationManager.createNotificationChannel(notificationChannel);
}
当按下操作按钮时,这是触发的代码部分:
if (action != null) {
switch (action) {
case "click":
alarmManager.cancel(cancelPendingIntent);
notificationManager.cancelAll();
player.stop();
changeAlarmValuesToOff();
break;
case "snooze":
alarmManager.cancel(cancelPendingIntent);
notificationManager.cancelAll();
player.stop();
setNewSnoozeAlarm();
break;
case "dismiss":
alarmManager.cancel(cancelPendingIntent);
notificationManager.cancelAll();
player.stop();
changeAlarmValuesToOff();
break;
default:
}
}
我也尝试过使用 cancel()
使用alarmRequestCode
这是用于警报和通知的唯一 ID,但仍然不起作用
一切工作正常,操作按需要执行,只是通知保持在那里并保持可点击并执行操作,如下面的屏幕截图所示
最佳答案
这里的问题不在于通知设置本身,问题在于通知在 Service
类中运行。当服务仍在运行时,无法取消通知。
我是如何解决这个问题的:在服务类中,执行操作后(我在调用 cancelAll()
的地方),我改为调用 stopSelf()
。然后重写 onDestroy()
并调用其中的 cancelAll()
!
干杯
关于android - cancelAll() 和 cancel() 不会忽略我的通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50163353/
在我的 Android 应用程序中,我运行一个计时器并在其他事件发生时取消它: class MyTimerTask extends TimerTask { override bool
如果有待处理的警报,PendingIntent.cancel() 如何影响 AlarmManager。 我应该对两个对象( Intent 和警报管理器)调用取消来取消警报吗?有人可以解释一下他们是如何
我正在研究如何取消闹钟,我遇到了这两种方法。应该在什么情况下使用哪一个,为什么?它们是一样的吗? 我目前正在这样做: Intent alarmIntent = new Intent(ChangeAla
我与一位同事讨论了在表单中取消的“网络标准”是什么。在我们的讨论中,我们以“更改密码”页面为例。我们设计了“发送”按钮和“取消”按钮。两者设计相同。 他声称在网络标准中,取消按钮不再是一个按钮,而是一
我一直在探索协程,我很惊讶地发现在 Job 上调用的 cancel 实际上并没有取消作业,而 cancel 在 scope 上调用会立即取消从 scope 启动的协程。谁能解释一下? 作业取消示例。
我正在使用 iOS 应用内购买。 我想请求最新的取消日期和取消总数,最好是 100% 正确的结果。 我正在使用 Apple 的 verifyreceipt 调用 ( https://developer
我在 ionic 2 应用程序中使用谷歌地图,我希望我的标记根据 map 的缩放级别显示/消失。所以我在函数“ionViewDidEnter”中添加了以下行: this.map.addList
在我们的网络应用程序中,我将 Angular-Materials $mdDialog 与确认对象一起使用。是否可以将按钮的顺序从取消-确定更改为确定-取消?并将初始焦点设置为取消按钮?也许通过 CSS
在我的表单“别名”的Form_beforeupdate()事件上,我有这个... If IsNull(Me.txtFName) And IsNull(Me.txtLName) Then MsgBox
在 QInputDialog 中,如何去掉 OK 和 Cancel 按钮中的图标? 注意取消和确定的图标。我查看了属性按钮,不知道如何删除它们。 最佳答案 解决方案的策略是先获取按钮,但是这些属于QD
我正在使用 MFMailComposeViewController Controller ,如下所示: MFMailComposeViewController *picker1 = [[MFMailC
当我尝试连接在不同机器上运行的对等点时出现此错误。我在订购者的 docker 日志中发现此错误。在不同机器上运行的 peer2 的 docker 日志中有错误 获取连接失败:无法连接到任何端点:[or
我注意到,当用户通过提示窗口输入文本时,提示将返回null并继续继续执行代码。我希望取消按钮按照其指示执行操作并取消。我尝试了几个 if 语句,包括: var x= prompt("Please en
我有一个定制对话框winform。在它上面,我有一个标签,文本框和2个按钮(“确定”和“取消”)。它还声明并定义重载的执行方法以传递不同的参数列表。 对话框Winform的调用方式如下: var th
我刚刚使用 Visual Studio 2017 安装了 Xamarin 并创建了一个新的空白应用程序。 当我按 F5 运行应用程序时,出现以下错误:构建已被取消。但是在构建解决方案时(ctrl+sh
Closed. This question needs debugging details。它当前不接受答案。 想改善这个问题吗?更新问题,以便将其作为on-topic用于堆栈溢出。 3年前关闭。 I
我在使用 NgRx 的 Angular 应用程序中使用轮询方案。 为了简化事情,我有如下内容: public stopPolling$ = createEffect(() => this.ac
在我读到的规范中: The bubbles and cancelable attributes must return the values they were initialized to. 我找不
我有一个 dataGetter 类,在其中加载必要的数据(部分 url 地址、电子邮件等),然后调用 AsyncTask。我使用一个抽屉菜单,其中每个 fragment 在创建时都会从新线程调用 da
我在使用 NgRx 的 Angular 应用程序中使用轮询方案。 为了简化事情,我有如下内容: public stopPolling$ = createEffect(() => this.ac
我是一名优秀的程序员,十分优秀!