0
對於我的代碼的這部分,我有一個時間表,它有一個布爾每週的每一天。如果那一天是真的,那麼當天就會有一個開始和結束的警報,並且這個警報應該在那一天的每個星期被觸發。如果我在測試期間的當天晚些時候,或者在一週後的某天晚些時候設置了鬧鐘,但是在任何其他時間,例如在已經發生的一週中的某一天,或者如果我在之前設置了鬧鐘比測試的日子,這兩個警報在創建時由於某種原因而關閉。下面是告警產生的代碼:AlarmManager未正確觸發
public void createNewAlarm(int i) //int correlates to position in list
{
for(int j = 0; j < 7; j++)
{
if(tempmainfrag.mainObjectList.returnSchedule(i).returnDays()[j]) //if this day of the week has an alarm
{
////beginning alarm stuff////
int alarmid = (int)System.currentTimeMillis(); //creates unique id for the alarm attached to the object
int adjustedday = j+2; //makes time for DAY_OF_WEEK where sunday = 1, monday = 2, etc.
if (adjustedday == 8)
adjustedday = 1;
Calendar startcal = Calendar.getInstance();
startcal.set(Calendar.DAY_OF_WEEK, adjustedday);
startcal.set(Calendar.HOUR_OF_DAY, tempmainfrag.mainObjectList.returnSchedule(i).returnTimes()[0]);
startcal.set(Calendar.MINUTE, tempmainfrag.mainObjectList.returnSchedule(i).returnTimes()[1]);
startcal.set(Calendar.SECOND, 0);
Intent intent = new Intent(this, SilenceHandler.class);
intent.putExtra("alarm_message", "Start!");
intent.putExtra("vibratemode", tempmainfrag.mainObjectList.returnSchedule(i).returnVibrate());
intent.setData((Uri.parse(String.valueOf(alarmid))));
PendingIntent pendintent = PendingIntent.getBroadcast(this, alarmid, intent, PendingIntent.FLAG_UPDATE_CURRENT);
tempmainfrag.mainObjectList.returnSchedule(i).addStartPendIntent(alarmid);
AlarmManager alarmman = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmman.setRepeating(AlarmManager.RTC_WAKEUP, startcal.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendintent);
//120000 means every 2 mins
////ending alarm stuff////
if(tempmainfrag.mainObjectList.returnSchedule(i).nextday) //if end alarm ends on next day instead of same day
{
adjustedday++;
if (adjustedday == 8)
adjustedday = 1;
}
alarmid = (int)System.currentTimeMillis(); //creates unique id for the alarm attached to the object
Calendar endcal = Calendar.getInstance();
endcal = Calendar.getInstance();
endcal.set(Calendar.DAY_OF_WEEK, adjustedday);
endcal.set(Calendar.HOUR_OF_DAY, tempmainfrag.mainObjectList.returnSchedule(i).returnTimes()[2]);
endcal.set(Calendar.MINUTE, tempmainfrag.mainObjectList.returnSchedule(i).returnTimes()[3]);
endcal.set(Calendar.SECOND, 0);
Intent intent2 = new Intent(this, SilenceHandler.class);
intent2.putExtra("alarm_message", "End!");
intent2.putExtra("vibratemode", tempmainfrag.mainObjectList.returnSchedule(i).returnVibrate());
intent2.setData((Uri.parse(String.valueOf(alarmid))));
PendingIntent pendintent2 = PendingIntent.getBroadcast(this, alarmid, intent2, PendingIntent.FLAG_UPDATE_CURRENT);
tempmainfrag.mainObjectList.returnSchedule(i).addEndPendIntent(alarmid);
AlarmManager alarmman2 = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmman2.setRepeating(AlarmManager.RTC_WAKEUP, endcal.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendintent2);
//120000 means every 2 mins
}
}
}
而且我不知道這是否是相關的或沒有,但這裏是報警刪除代碼,雖然這部分似乎是工作的罰款,據我所知:
public void deleteAlarm(int i)
{
AlarmManager alarmman = (AlarmManager)getSystemService(ALARM_SERVICE);
Log.i("mydebug","Deleting alarm: The pending intent list null/not null is: " + tempmainfrag.mainObjectList.returnSchedule(i).startAlarmList);//.pendintentlist.size());
//delete start alarms
if (tempmainfrag.mainObjectList.returnSchedule(i).startAlarmList != null)
{
Log.i("mydebug","Cancelling start alarm...");
//cancels all alarms
for (int j = 0; j < tempmainfrag.mainObjectList.returnSchedule(i).startAlarmList.size(); j++)
{
Intent intent = new Intent(this, SilenceHandler.class);
intent.putExtra("starttime",tempmainfrag.mainObjectList.returnSchedule(i));
intent.putExtra("alarm_message", "Start!");
intent.setData((Uri.parse(String.valueOf(tempmainfrag.mainObjectList.returnSchedule(i).startAlarmList.get(j)))));
Log.i("mydebug","Alarm number " + (j+1) + " being cancelled.");
PendingIntent pendintent = PendingIntent.getBroadcast(this, tempmainfrag.mainObjectList.returnSchedule(i).startAlarmList.get(j), intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmman.cancel(pendintent);
}
}
//delete end alarms
if (tempmainfrag.mainObjectList.returnSchedule(i).endAlarmList != null)
{
Log.i("mydebug","Cancelling end alarm...");
//cancels all alarms
for (int j = 0; j < tempmainfrag.mainObjectList.returnSchedule(i).endAlarmList.size(); j++)
{
Intent intent = new Intent(this, SilenceHandler.class);
intent.putExtra("endtime",tempmainfrag.mainObjectList.returnSchedule(i));
intent.putExtra("alarm_message", "End!");
intent.setData((Uri.parse(String.valueOf(tempmainfrag.mainObjectList.returnSchedule(i).endAlarmList.get(j)))));
Log.i("mydebug","Alarm number " + (j+1) + " being cancelled.");
PendingIntent pendintent = PendingIntent.getBroadcast(this, tempmainfrag.mainObjectList.returnSchedule(i).endAlarmList.get(j), intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmman.cancel(pendintent);
}
}
//deletes alarm intents from object
tempmainfrag.mainObjectList.returnSchedule(i).deleteIntents();
}