如何从AlarmManager取消警报

编程入门 行业动态 更新时间:2024-10-25 04:26:29
本文介绍了如何从AlarmManager取消警报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我遇到了同样的问题. 使用cancel()从Android的AlarmManager删除警报-Android

I have met same issue like this . Delete alarm from AlarmManager using cancel() - Android

" 我试图用两种不同的方法来创建和删除警报,这两种方法在应用程序的不同时刻都被调用.逻辑.

" I'm trying to create and delete an alarm in two different methods which are both called at different moments in the application. logic.

但是,当我调用AlarmManager的cancel()方法时,该警报不会被删除."

However when I call AlarmManager's cancel() method, the alarm isn't deleted."

要设置:

Intent myIntent = new Intent(getApplicationContext(), SessionReceiver.class); PendingIntent pendingIntent = PendingIntent.getBroadcast( getApplicationContext(), 1, myIntent, 0); AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); alarmManager.set(AlarmManager.RTC, now.getTimeInMillis(), pendingIntent);

为了删除:

AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); Intent myIntent = new Intent(getApplicationContext(), SessionReceiver.class); PendingIntent pendingIntent = PendingIntent.getBroadcast( getApplicationContext(), 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT); alarmManager.cancel(pendingIntent);

但这不会删除已注册的警报. 提前致谢.

But this doesn't remove an alarm registered. Thanks in advance.

推荐答案

PendingIntent必须与启动AlarmManager时完全一样地创建,并且看起来主要问题是您使用的是不同的requestCode(零而不是一).

The PendingIntent needs to be created exactly as it was when you start the AlarmManager, and it looks like the main issue is that you're using a different requestCode (zero instead of one).

要快速修复,此方法应该有效:

For a quick fix, this should work:

AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); Intent myIntent = new Intent(getApplicationContext(), SessionReceiver.class); PendingIntent pendingIntent = PendingIntent.getBroadcast( getApplicationContext(), 1, myIntent, 0); alarmManager.cancel(pendingIntent);

要使用PendingIntent.FLAG_UPDATE_CURRENT标志,请参见下文:

In order to use PendingIntent.FLAG_UPDATE_CURRENT flag, see below:

设置:

Intent myIntent = new Intent(getApplicationContext(), SessionReceiver.class); PendingIntent pendingIntent = PendingIntent.getBroadcast( getApplicationContext(), 1, myIntent, PendingIntent.FLAG_UPDATE_CURRENT); AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); alarmManager.set(AlarmManager.RTC, now.getTimeInMillis(), pendingIntent);

取消:

AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); Intent myIntent = new Intent(getApplicationContext(), SessionReceiver.class); PendingIntent pendingIntent = PendingIntent.getBroadcast( getApplicationContext(), 1, myIntent, PendingIntent.FLAG_UPDATE_CURRENT); alarmManager.cancel(pendingIntent);

更多推荐

如何从AlarmManager取消警报

本文发布于:2023-11-27 01:37:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1636043.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:警报   AlarmManager

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!