0

对于我的这部分代码,我有一个时间表,其中包含一周中每一天的布尔值。如果那一天是真的,那么就有那一天的开始和结束警报,并且这些警报应该在每周的那一天触发。如果我在测试期间将警报设置为当天晚些时候,或者一周中的晚些时候,但是对于任何其他时间,例如已经发生的一周中的一天,或者如果我将任一警报设置为早些时候与测试时相比,由于某种原因,两个警报在创建时都会响起。这是创建警报的代码:

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();
}
4

1 回答 1

1

在进一步了解了 AlarmManager 系统后,我最终找到了解决办法。添加这些行可以纠正它:

Calendar checkdate = Calendar.getInstance();    //used so that alarm set to earlier in the week doesnt go off
if (startcal.before(checkdate)) //see if alarm is for earlier in week
    {
        startcal.add(Calendar.DATE, 7); //makes alarm trigger next time it becomes that day instead of immediately
    }
于 2013-03-22T00:03:58.837 回答