简体   繁体   English

如何从 AlarmManager 取消警报

[英]How to cancel alarm from AlarmManager

I have met same issue like this.我遇到了同样的问题。 Delete alarm from AlarmManager using cancel() - Android 使用 cancel() 从 AlarmManager 中删除警报 - 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. “我正在尝试以两种不同的方法创建和删除警报,这两种方法都在应用程序的不同时刻调用。逻辑。

However when I call AlarmManager's cancel() method, the alarm isn't deleted."但是,当我调用 AlarmManager 的 cancel() 方法时,警报并没有被删除。”

In order to set:为了设置:

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

In order to delete:为了删除:

    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.提前致谢。

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). 需要创建PendingIntent,就像启动AlarmManager时一样,看起来主要的问题是你使用的是另一个requestCode(零而不是一个)。

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

In order to use PendingIntent.FLAG_UPDATE_CURRENT flag, see below: 要使用PendingIntent.FLAG_UPDATE_CURRENT标志,请参阅以下内容:

Setting: 设置:

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

Cancelling: 取消:

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

Initially for me also not worked,After Seeing many posts i realized that the pending intent to be canceled should be same as the original pending intent that was used to schedule alarm. 最初对我来说也没有用,在看到很多帖子之后我意识到要取消的未决意图应该与用于安排警报的原始待定意图相同。 The pending intent to be cancelled should have set to same action and same data fields,if any have those were used to set the alarm.After setting the same ACTION and data values though i'm not using them,only cancelled the Alarm. 要取消的未决意图应设置为相同的操作和相同的数据字段,如果有的话用于设置警报。设置相同的ACTION和数据值虽然我没有使用它们,但只取消了警报。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM