0

コードのこの部分では、曜日ごとにブール値を持つスケジュールがあります。その日が true の場合、その日の開始アラームと終了アラームがあり、これらのアラームは毎週その日にトリガーされます。テスト中に現在の日の後半にアラームを設定した場合、または週の後半にアラームを設定した場合に機能しますが、すでに発生した曜日のように他の時間にアラームを設定した場合、またはいずれかのアラームを以前に設定した場合に機能しますテスト時より当日、なぜか作成時に両方のアラームが鳴る。アラーム作成のコードは次のとおりです。

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 に答える