5

我是整个 Android 环境的新手,我通常有一些疑问,也许你可以考虑非常基础的知识和有点愚蠢。我会尽力解释我的疑问以及为什么我要让我理解。

我正在做一个应用程序,您可以在其中设置通知以提醒您想要的学者课程。我已经完成了一个扩展 BroadcastReceiver 的类,因此它可以在设备启动后重置所有警报。我有一个数据库,用于保存有关警报的信息:类、必须配置的时间等。我检索所有警报并将它们设置为 alarmManager:

intent = new Intent(ctxt.getApplicationContext(), Notificacion.class);
intent.putExtra("TAG", tag);
intent.putExtra("SUBJECT", cursor2.getString(0));
intent.putExtra("AULA", cursor2.getString(1));                  
displayIntent = PendingIntent.getBroadcast(ctxt, Integer.parseInt(tag), intent, PendingIntent.FLAG_UPDATE_CURRENT );               
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY*7, displayIntent);

好吧,我想这应该可以正常工作,直到这里。问题是,当您使用该应用程序并想要设置通知时,您是从“Schedule.class”类中进行的,因此意图将具有以下上下文:

Intent intent = new Intent(getApplicationContext(), Notification.class);
PendingIntent pend = PendingIntent.getBroadcast(this, Integer.parseInt(tag), intent, PendingIntent.FLAG_UPDATE_CURRENT);

在应用程序中,您可以删除警报,您必须调用 alarmManager.cancel(pend) 才能执行此操作。所以我怀疑它是否能够取消它。

如果上下文不同,它将找不到与挂起意图的匹配项,因为它是根据我在 BroadCastReceiver (ctxt) 扩展中获得的上下文设置的,并且警报是根据我从 Schedule.class 获得的上下文设置的.

那么..应用程序上下文是否总是相同的?我知道设置上下文是为了向其他类提供有关正在发生的事情的信息,但我不确定 Intent 过滤器是否会区分上下文的给出位置。

先感谢您!

4

1 回答 1

2

Looking at the AlarmManager documentation for the cancel method you're using:

public void cancel (PendingIntent operation)

Added in API level 1

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.

So, the Intent.filterEquals documentation says the following:

public boolean filterEquals (Intent other)

Added in API level 1

Determine if two intents are the same for the purposes of intent resolution (filtering). That is, if their action, data, type, class, and categories are the same. This does not compare any extra data included in the intents.

I can't think of any reason why the action, data, type, class, or category would be different from one explicit Intent to another (unless, obviously you went out of your way to change those things). The contexts do not appear to be in the criteria for the matching, so I think you can be fairly confident that it will be cancelled no matter which context was used to create it in the first place.

于 2012-11-28T22:01:50.900 回答