2012-07-24 49 views
0

我使用AlarmManager安排多个taks,即在10:56的任务1,在11:24的任务2等等。 下面的代码:为AlarmManager设置多个Intents

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

下面是奇怪的事情:如果我设置一个报警器,它的工作做好。如果我设置了两个,只会触发最后一个闹钟。 所以,我想问题是,当我设置第二,第三...警报,先前被覆盖。

从开发者文档:

如果已经有这个意向计划(有两个意图的 平等的filterEquals被定义(意图))的报警,然后 将被删除,取而代之这个。

我去的来源,下面的方法实现:

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

所以,据我所见,没有提及额外的。实际上,我是依靠这个:

intent.putExtra(EXTRA_COMMAND_ID, command.id); 

但是standart实现并不比较exras。 因此,当我安排多个意图时,他们被比较相等并得到重写!

实际的问题:

如何重写filterEquals(Intent),这样我就可以区分基于额外意图?

这里是我的实现:

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未被调用。

回答

0

结束了与此:

pendingIntent = PendingIntent.getService(context, UNIQUE_ID, intent, PendingIntent.FLAG_ONE_SHOT); 

提供了第二个参数getService做的工作。