2011-04-25 136 views
0

我有待定的意圖,設置警報,並從數據庫中獲取參數,如行ID和時間。我想取消警報,所以我會通過發送另一個掛起的意圖與相同的信息,然後取消(我想從不同的文件中取消它)。我只允許在任何時候設置一個鬧鐘,因爲這是我的應用程序的工作方式,因爲從該待處理的意圖中只設置了一個鬧鐘,無論如何我都可以取消所有該意圖?Android正在等待意圖?

回答

0

我有待定的意圖,設置警報,它從數據庫中獲取參數,如行ID和時間。

PendingIntent上沒有「參數」。我將把這解釋爲「額外」的含義。

我想取消警報,所以我會這樣做,通過發送另一個掛起的意圖與相同的信息,然後取消(我想從不同的文件中取消它)。

它不是「發送」而是「創建」。否則,是的,這是正確的。

我可以只取消所有意圖?

只有一個警報。

在報警安排方面,附加並不重要。如果在PendingIntent(PI1)中包含Intent(I1)並使用它來安排警報,並且稍後如果使用相同的組件/動作/數據/類型創建Intent(I2),請將其包裝在PendingIntent( PI2)和cancel()報警,它將取消PI1報警。同樣,如果您使用PI2安排新的警報,它將刪除舊的PI1警報。

+0

非常感謝,你的答案被清除了很多關於如何取消:) – Bob 2011-04-25 23:53:03

3

因爲我相信代碼樣本中傳達出點,見下圖:

/* 
* An alarm can invoke a broadcast request 
* starting at a specified time and at 
* regular intervals. 
*/ 
public void sendRepeatingAlarm() 
{ 
    Calendar cal = Utils.getTimeAfterInSecs(30); 

    String s = Utils.getDateTimeString(cal); 
    this.mReportTo.reportBack(tag, "Schdeduling Repeating alarm in 5 sec interval starting at: " + s); 

    //Get an intent to invoke TestReceiver class 
    Intent intent = new Intent(this, TestReceiver.class); 
    intent.putExtra("message", "Repeating Alarm"); 

    PendingIntent pi = this.getDistinctPendingIntent(intent, 2); 

    // Schedule the alarm! 
    AlarmManager am = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE); 

    am.setRepeating(AlarmManager.RTC_WAKEUP, 
      cal.getTimeInMillis(), 
      5*1000, //5 secs 
      pi); 
} 

protected PendingIntent getDistinctPendingIntent(Intent intent, int requestId) 
{ 
    PendingIntent pi = 
     PendingIntent.getBroadcast(
      this,   //context 
      requestId, //request id 
      intent,  //intent to be delivered 
      0); 

    //pending intent flags 
    //PendingIntent.FLAG_ONE_SHOT);  
    return pi; 
} 

/* 
* An alarm can be stopped by canceling the intent. 
* You will need to have a copy of the intent 
* to cancel it. 
* 
* The intent needs to have the same signature 
* and request id. 
*/ 
public void cancelRepeatingAlarm() 
{ 
    //Get an intent to invoke TestReceiver class 
    Intent intent = new Intent(this, TestReceiver.class); 

    //To cancel, extra is not necessary to be filled in 
    //intent.putExtra("message", "Repeating Alarm"); 

    PendingIntent pi = this.getDistinctPendingIntent(intent, 2); 

    // Schedule the alarm! 
    AlarmManager am = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE); 
    am.cancel(pi); 
} 
+0

如果我有相關提醒10該應用意味着,我如何管理唯一的ID。?有沒有關於管理身份證的想法? – Karthick 2013-09-27 07:24:51

+0

@Karthick有沒有解決這個問題的方法? – Sauron 2015-06-11 01:38:42