AlarmManager未正确触发

时间:2013-03-21 23:31:04

标签: android 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();
}

1 个答案:

答案 0 :(得分: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
    }