2011-10-21 104 views
1

我面臨着取消特定警報的問題。我正在使用報警管理器SET方法設置多個報警。但我無法取消特定的警報。如果我取消,則取消之前設置的所有警報。我正在使用單個ALARM MANAGER ID來設置多個警報。如何取消特定警報?

保存方法

private void saveState() 
{ 
    // String date = mDateText.getText().toString(); 
    String title = mTitleText.getText().toString(); 
    String body = mBodyText.getText().toString(); 
    String sdate = sDateText.getText().toString(); 
    String stime = sTimeText.getText().toString(); 
    String rdate = rDateText.getText().toString(); 
    String rtime = rTimeText.getText().toString(); 

    String appRemDate = ((Button)findViewById(R.id.enterdaterem)).getText().toString(); 
    String appRemTime = ((Button)findViewById(R.id.entertimerem)).getText().toString(); 


    Calendar cal = Calendar.getInstance(); 

    //setting date & month 
    if(appRemDate.substring(0,2).endsWith("-") && appRemDate.substring(2,4).endsWith("-")) 
    { 

     cal.set(Calendar.YEAR, Integer.parseInt(appRemDate.substring(4,8))); 
     cal.set(Calendar.MONTH, Integer.parseInt(appRemDate.substring(2,3)) -1); 
     cal.set(Calendar.DATE, Integer.parseInt(appRemDate.substring(0,1))); 
    } 
    else if(!appRemDate.substring(0,2).contains("-") && !appRemDate.substring(3,5).contains("-")) 
    { 

     cal.set(Calendar.YEAR, Integer.parseInt(appRemDate.substring(6,10))); 
     cal.set(Calendar.MONTH, Integer.parseInt(appRemDate.substring(3,5)) -1); 
     cal.set(Calendar.DATE, Integer.parseInt(appRemDate.substring(0,2))); 
    } 
    else if(appRemDate.substring(0,2).endsWith("-") && !appRemDate.substring(2,4).endsWith("-")) 
    { 
     cal.set(Calendar.YEAR, Integer.parseInt(appRemDate.substring(5,9))); 
     cal.set(Calendar.MONTH, Integer.parseInt(appRemDate.substring(2,4)) -1); 
     cal.set(Calendar.DATE, Integer.parseInt(appRemDate.substring(0,1))); 
    } 
    else 
    { 
     cal.set(Calendar.YEAR, Integer.parseInt(appRemDate.substring(5,9))); 
     cal.set(Calendar.MONTH, Integer.parseInt(appRemDate.substring(3,4)) -1); 
     cal.set(Calendar.DATE, Integer.parseInt(appRemDate.substring(0,2))); 
    } 
    cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(appRemTime.substring(0, 2))); 

    cal.set(Calendar.MINUTE, Integer.parseInt(appRemTime.substring(3, 5))); 

    cal.set(Calendar.SECOND, 0); 

    if (mRowId == null) 
    { 
     long id = mDbHelper.createNote( title, body, sdate, stime, rdate, rtime); 
     invokeAlaram(cal.getTimeInMillis(), id); 
     if (id > 0) 
     { 
      mRowId = id; 
     } 
    } 
    else 
    { 
     long id = mDbHelper.updateNote(mRowId, title, body, sdate, stime, rdate, rtime); 
     invokeAlaram(cal.getTimeInMillis(), id); 
    } 
} 

INVOKE報警方式

private void invokeAlaram(long invokeTime, long id) 
{ 

    AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE); 

    Intent i = new Intent(this, MyAlarmServiceForAppointments.class); 

    i.putExtra("rowId", String.valueOf(id)); 
    am.set(AlarmManager.RTC_WAKEUP, invokeTime, PendingIntent.getService(this , (int) System.currentTimeMillis() ,i , 0)); 

} 

誰能幫我如何取消特定的報警。

回答

10

當你經過系統毫秒時間要求的代碼到的getService()喜歡這裏

am.set(AlarmManager.RTC_WAKEUP, invokeTime, PendingIntent.getService(this , (int) System.currentTimeMillis() ,i , 0)); 

而不是使用你的系統時間通過你的唯一的數值認爲是請求爲特定目的不同。

現在你必須保持在此基礎上這一切請求的代碼,你可以取消報警

注:由於Intent對象傳遞到的PendingIntent必須是相同的定義,你在設定的定義解除警報時間

am.set(AlarmManager.RTC_WAKEUP, invokeTime, PendingIntent.getService(this , 1 ,i , 0)); 

am.set(AlarmManager.RTC_WAKEUP, invokeTime, PendingIntent.getService(this , 2 ,i , 0)); 

現在取消

am.cancel(PendingIntent.getService(this , 2 ,i , 0));