简体   繁体   English

在AlarmManager中取消警报

[英]Canceling an alarm in AlarmManager

In my code, I create an alarm as follows: 在我的代码中,我创建了一个警报,如下所示:

AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent("mypackage.START_MONITORING_SERVICE");
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
long timeForNextStart = System.currentTimeMillis() + elapsedTime;
am.set(AlarmManager.RTC_WAKEUP, timeForNextStart, pi);

To cancel the alarm I do this: 要取消警报,我可以这样做:

AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent("mypackage.START_MONITORING_SERVICE");
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
am.cancel(pendingIntent);

My question is whether this is the correct way to cancel ALL alarms of the same intent. 我的问题是这是否是取消具有相同意图的所有警报的正确方法。 The docs say: 文档说:

Remove any alarms with a matching Intent. 删除所有具有匹配意图的警报。 Any alarm, of any type, whose Intent matches this one (as defined by filterEquals(Intent)), will be canceled. 任何警报,其意图与该警报相匹配(由filterEquals(Intent)定义)的任何类型的警报都将被取消。

I'm not exactly sure what defines a "matching intent". 我不确定是什么定义了“匹配意图”。 If I create multiple alarms with the above code and then peform the cancel as shown, will it cancel ALL of the alarms I created? 如果我使用上面的代码创建了多个警报,然后执行如图所示的取消,它将取消我创建的所有警报吗?

您应该使用PendingIntent标志==> PendingIntent.FLAG_CANCEL_CURRENT第四个参数创建endingIntent实例方法。

PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent,PendingIntent.FLAG_CANCEL_CURRENT);

Try this 尝试这个

AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

    Intent updateServiceIntent = new Intent(context, MyPendingIntentService.class);
    PendingIntent pendingUpdateIntent = PendingIntent.getService(context, 0, updateServiceIntent, 0);

    // Cancel alarms
    try {
        alarmManager.cancel(pendingUpdateIntent);
    } catch (Exception e) {
        Log.e(TAG, "AlarmManager update was not canceled. " + e.toString());
    }

My question is whether this is the correct way to cancel ALL alarms of the same intent? 我的问题是,这是否是取消所有意图相同的警报的正确方法?

I dont know if you aware of requestCode should be unique in for each pendingIntent for following syntax 我不知道你是否意识到requestCode应该是为每一个独特pendingIntent的语法如下

public static PendingIntent getService (Context context, int requestCode, Intent intent, int flags)

I'm not exactly sure what defines a "matching intent". 我不确定是什么定义了“匹配意图”。

Each pending intent map with its provided intent to trigger and each request code map with that intent . 每个未决的意图映射及其提供的触发意图,每个请求代码映射均具有该意图。

We can say it can be same requestcode for two different pendingIntent which ideally having two different intents . 我们可以说,它可以是相同的requestcode两个不同pendingIntent在理想情况下有两种不同的intents

Remember , here if PendingIntent have a same requestCode for a same intent then it can be override or cancel according to int flags 请记住,这里如果PendingIntent对于相同的intent具有相同的requestCode ,则可以根据int flags覆盖或取消它

If I create multiple alarms with the above code and then peform the cancel as shown, will it cancel ALL of the alarms I created? 如果我使用上面的代码创建了多个警报,然后执行如图所示的取消,它将取消我创建的所有警报吗?

To create a multiple alarm you have to give a different requestCode with the same intent . 要创建多个警报,您必须提供具有相同intent的其他requestCode If you want to cancel those created alarm then you have to cancel them according requestCode 如果要取消那些已创建的警报,则必须根据requestCode取消它们

For example 例如

To Create :

AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
For (int i= 0 ; i <5 ; i++){
PendingIntent contentIntent = PendingIntent.getBroadcast(TaskReminder.this, i,
                            new Intent(),PendingIntent.FLAG_UPDATE_CURRENT);
 alarmManager.set(AlarmManager.RTC_WAKEUP, timeForNextStart, contentIntent);

}

To cancel :

For (int i= 0 ; i <5 ; i++){
//Note here ' i ' is the requestCode which map each pendingIntent with there provided intent 
PendingIntent contentIntent = PendingIntent.getBroadcast(TaskReminder.this, i,
                            new Intent(),PendingIntent.FLAG_UPDATE_CURRENT);
 alarmManager.cancel(contentIntent);

}

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

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