设置AlarmManager多意图 [英] Setting multiple Intents for AlarmManager
问题描述
我用AlarmManager 10:56在11:24等安排多个TAKS,即任务1,任务2。
这里的code:
I use AlarmManager to schedule multiple taks, i.e. Task 1 at 10:56, Task 2 at 11:24 and so on. Here's the code:
intent = new Intent(ACTION_RECORDER_START);
intent.putExtra(EXTRA_COMMAND_ID, command.id);
pendingIntent = PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);
alarmManager.set(AlarmManager.RTC_WAKEUP, command.start, pendingIntent);
下面是奇怪的事情:如果我设置一个报警,但它的工作做好。如果我设置两个,只有最后一个报警被触发。
所以,我想的问题是,当我设置第二,第三......报警,previous被覆盖。
Here's the strange thing: If I set one alarm, it does job well. If I set two, only the last alarm is fired. So, I guess the problem is that when I set second, third...alarm, previous is overriden.
从开发文档:
如果已经有这个意向预定的报警(用
两个目的平等被定义filterEquals(意向)),然后
它将由这一个被移除和更换。
If there is already an alarm for this Intent scheduled (with the equality of two intents being defined by filterEquals(Intent)), then it will be removed and replaced by this one.
我去源,下面的方法实现:
I went to the sources, here's the implementation of that method:
public boolean filterEquals(Intent other) {
if (other == null) {
return false;
}
if (mAction != other.mAction) {
if (mAction != null) {
if (!mAction.equals(other.mAction)) {
return false;
}
} else {
if (!other.mAction.equals(mAction)) {
return false;
}
}
}
if (mData != other.mData) {
if (mData != null) {
if (!mData.equals(other.mData)) {
return false;
}
} else {
if (!other.mData.equals(mData)) {
return false;
}
}
}
if (mType != other.mType) {
if (mType != null) {
if (!mType.equals(other.mType)) {
return false;
}
} else {
if (!other.mType.equals(mType)) {
return false;
}
}
}
if (mPackage != other.mPackage) {
if (mPackage != null) {
if (!mPackage.equals(other.mPackage)) {
return false;
}
} else {
if (!other.mPackage.equals(mPackage)) {
return false;
}
}
}
if (mComponent != other.mComponent) {
if (mComponent != null) {
if (!mComponent.equals(other.mComponent)) {
return false;
}
} else {
if (!other.mComponent.equals(mComponent)) {
return false;
}
}
}
if (mCategories != other.mCategories) {
if (mCategories != null) {
if (!mCategories.equals(other.mCategories)) {
return false;
}
} else {
if (!other.mCategories.equals(mCategories)) {
return false;
}
}
}
return true;
}
所以,就我看,没有提及额外的。其实,我是靠这个:
So, as far as I see, no mention of extras. In fact, I was relying on this:
intent.putExtra(EXTRA_COMMAND_ID, command.id);
但非标准实现不比较exras。
所以,当我安排多意图,他们相比平等并得到重写!
but standart implementation doesn't compare exras. So, when I schedule multiple intents, they are compared equal and get overriden!
实际问题:
我如何重写 filterEquals(意向)
,这样我就可以区分的基础上额外意图?
How do I override filterEquals(Intent)
, so that I can distinguish Intents based on Extras?
下面是我实现的:
static class AlarmIntent extends Intent{
public AlarmIntent(String action){
super(action);
}
@Override
public boolean filterEquals(Intent other){
if(super.filterEquals(other)){
long id = getExtras().getLong(AudioRecorder.EXTRA_COMMAND_ID, -1);
long otherId = other.getExtras().getLong(AudioRecorder.EXTRA_COMMAND_ID, -1);
if(id == otherId){
return true;
}
}
return false;
}
}
但在我看来,这是行不通的。我认为,被覆盖的 filterEquals
不被调用。
推荐答案
结束了这一点:
pendingIntent = PendingIntent.getService(context, UNIQUE_ID, intent, PendingIntent.FLAG_ONE_SHOT);
提供了第二个参数的getService
做的工作。
这篇关于设置AlarmManager多意图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!