2011-10-21 18 views
1

特定のアラームをキャンセルすることに問題があります。アラームマネージャのSETメソッドを使用して複数のアラームを設定しています。しかし、私は特定のアラームをキャンセルすることはできません。キャンセルすると、以前に設定したアラームがすべてキャンセルされます。私は複数のアラームを設定するために単一のALARM MANAGER IDを使用しています。特定のアラームをキャンセルするには?

SAVE METHOD

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のALARM METHOD

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

} 

誰もが特定のアラームを解除する方法を手伝ってくれる。ここ

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

代わりのシステム時刻を使用して、あなたは、要求が特定の意図のために異なっていたとして考えるあなたの固有の番号を渡すために持っているよう

答えて

10

あなたがのgetService(にリクエストコードのためのシステムのミリ秒の時間を渡すよう) 。

今、あなたはこれに基づいて、このすべての要求コードを維持する必要がありますが、アラーム

注意を取り消すことができます:インテントオブジェクトとして、あなたは、あなたがセットで定義されたアラームを解除するために同じ定義する必要がありますPendingIntentに渡します時間キャンセル

am.cancel(PendingIntent.getService(this , 2 ,i , 0)); 
ようになりまし

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

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

ので、

関連する問題