2012-07-18 47 views
0

我有保存在TimeInterval對象中的每週時間表,它們工作正常。我構建了一個循環,因此我可以爲每個人設置一個鬧鐘。我的代碼不起作用,我錯過了什麼?Android警報管理器循環中的多個待定目標

此外,我如何從另一個線程的BroadcastReceiver取消這些警報?

for(ArrayList<TimeInterval> day : days){ 
     for(TimeInterval ti : day){ 
      numberofintents++; 
      Intent i = new Intent(getApplicationContext(),ChangeMode.class); 
      i.putExtra("ringermode", ti.getRingerMode()); 

      PendingIntent pending = PendingIntent.getService(getApplicationContext(), id, i,PendingIntent.FLAG_CANCEL_CURRENT); 
      Calendar cal = Calendar.getInstance(); 
      cal.set(Calendar.MINUTE,ti.getMinute()%60); 
      cal.set(Calendar.HOUR_OF_DAY,ti.getMinute()/60); 
      cal.set(Calendar.DAY_OF_WEEK,dayofweek); 
      Log.d(TAG, cal.get(Calendar.HOUR_OF_DAY)+":"+cal.get(Calendar.MINUTE)); 
      service.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 1000*60*60*24*7, pending); 
      id++; 
     } 
     dayofweek++; 
    } 

回答

0

首先要掛起的意圖的一個數組列表,

public static ArrayList <PendingIntent> intentArray= new ArrayList <PendingIntent>(); 

再加入未決的意圖,以數組列表,

for(ArrayList<TimeInterval> day : days){ 
    for(TimeInterval ti : day){ 
     numberofintents++; 
     Intent i = new Intent(getApplicationContext(),ChangeMode.class); 
     i.putExtra("ringermode", ti.getRingerMode()); 

     PendingIntent pending = PendingIntent.getService(getApplicationContext(), id, i,PendingIntent.FLAG_CANCEL_CURRENT); 

     intentarray.add(pending); 

     Calendar cal = Calendar.getInstance(); 
     cal.set(Calendar.MINUTE,ti.getMinute()%60); 
     cal.set(Calendar.HOUR_OF_DAY,ti.getMinute()/60); 
     cal.set(Calendar.DAY_OF_WEEK,dayofweek); 


    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 
    alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 
      pendingIntent); 


     id++; 
    } 
    dayofweek++; 
} 

現在等待付款的意圖都存儲在數組列表。

如果要取消鬧鈴,

alarmManager.cancel(intentArray.get(i));