android - 为什么这个 android 取消警报不起作用?

标签 android

这是设置闹钟的原因

    public void setSilent(Long taskId, Calendar when){
     Intent i = new Intent(mContext, SilentReceiver.class);
     PendingIntent pi = PendingIntent.getBroadcast(mContext, 1 , i, PendingIntent.FLAG_ONE_SHOT);
     mAlarmManager.set(AlarmManager.RTC_WAKEUP, when.getTimeInMillis(), pi);

它接受一个 id 和日期以用于 Intent 。 当用户单击删除时,我正在另一个文件中取消它。使用

Intent i = new Intent(null, SilentReceiver.class);
         PendingIntent pi = PendingIntent.getBroadcast(null, 1 , i, PendingIntent.FLAG_ONE_SHOT);

因为它具有与第一个请求代码相同的请求代码,还是我做错了什么?

最佳答案

您必须保存对第一个 PendingIntent 的引用。这样你就可以使用:

mAlarmManager.cancel(pi);

并且应该取消警报。

这个答案已经重复了很多次。提问前请先搜索。

关于android - 为什么这个 android 取消警报不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5784164/

相关文章:

android - 如何在 Android 中将 gif 图像添加到 TextView 或 EditText

android - fragment 已创建但在 listView 的 itemClick 上不可见。卡在这里

android - 如何在Android状态栏通知中显示正确的当前时间?

android - WebView 使用 VALIDATION_ERROR_DESERIALIZATION_FAILED 使我的应用程序崩溃

android - 回收站 View 点击监听器

android - 来自不同项目的 AIDL 服务

android - 使用 cordova 从 android 项目更改构建名称

java - 在 Facebook 上发布图片不起作用

android - Flutter MediaQuery.of(context).size.width 检查设备大小总是在 iOS 模拟器中返回相同的错误值

android webview 渲染显示一个白页