2012-07-24 113 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做的工作。