7

所以基本上我有这个代码,时间返回 24 小时时间并每天重复警报。

public setAlarm(String time, Context context){
    String[] strTime;

    strTime = time.split(":");

    int hour, min, sec;
    //set when to alarm
    hour = Integer.valueOf(strTime[0]);
    min = Integer.valueOf(strTime[1]);
    sec = 0;

    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.HOUR_OF_DAY, hour);
    cal.set(Calendar.MINUTE, min);
    cal.set(Calendar.SECOND, sec);
    //Create a new PendingIntent and add it to the AlarmManager
    Intent intent = new Intent(context, AlarmReceiverActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 19248, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager am = (AlarmManager) context.getSystemService(Activity.ALARM_SERVICE);
    am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
}

无论如何,问题是当我在上午 9 点设置闹钟时,时间是上午 9 点 10 分,闹钟会响起。为什么?如果设置超过系统时间,我希望它不会发出警报。前任。将闹钟设置为上午 9 点,而系统时间为上午 9 点 10 分

4

3 回答 3

21

我现在开始工作了。我添加了闹钟时间和当前时间的检查器。

public setAlarm(String time, Context context){
        String[] strTime;

        strTime = time.split(":");

        int hour, min, sec;
        //set when to alarm
        hour = Integer.valueOf(strTime[0]);
        min = Integer.valueOf(strTime[1]);
        sec = 0;

        long _alarm = 0;
        Calendar now = Calendar.getInstance();
        Calendar alarm = Calendar.getInstance();
        alarm.set(Calendar.HOUR_OF_DAY, hour);
        alarm.set(Calendar.MINUTE, min);
        alarm.set(Calendar.SECOND, sec);

        if(alarm.getTimeInMillis() <= now.getTimeInMillis())
            _alarm = alarm.getTimeInMillis() + (AlarmManager.INTERVAL_DAY+1);
        else
            _alarm = alarm.getTimeInMillis();               

        //Create a new PendingIntent and add it to the AlarmManager
        Intent intent = new Intent(context, AlarmReceiverActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 19248, intent, PendingIntent.FLAG_CANCEL_CURRENT);
        AlarmManager am = (AlarmManager) context.getSystemService(Activity.ALARM_SERVICE);
        am.setRepeating(AlarmManager.RTC_WAKEUP, _alarm, AlarmManager.INTERVAL_DAY, pendingIntent);
    }
于 2013-07-16T18:17:45.370 回答
3

当我尝试它然后找出解决方案时,接受的答案是错误的。

这是错误的,因为正如您在此处看到的: Calendar now = Calendar.getInstance(); Calendar alarm = Calendar.getInstance(); alarm.set(Calendar.HOUR_OF_DAY, hour); alarm.set(Calendar.MINUTE, min); alarm.set(Calendar.SECOND, sec);

无论如何,“现在日历”和“日历警报”将始终相同,因为它们在代码中几乎完全相同的位置执行相同的操作,因此 Calendar.getInstance() 将始终相同。

解决方案是这样的

    long _alarm;
    Calendar now = Calendar.getInstance();
    long oldtimer = now.getTimeInMillis();

    cal.set(Calendar.HOUR_OF_DAY, Hours2int);
    cal.set(Calendar.MINUTE, minutes2int);
    cal.set(Calendar.SECOND, cur_cal.get(Calendar.SECOND));
    cal.set(Calendar.MILLISECOND, cur_cal.get(Calendar.MILLISECOND));
    cal.set(Calendar.DATE, cur_cal.get(Calendar.DATE));
    cal.set(Calendar.MONTH, cur_cal.get(Calendar.MONTH));

    //Calendar alarm = Calendar.getInstance();

    long newtimer = cal.getTimeInMillis();


    if(newtimer < oldtimer) {
        //do the thing
    }
于 2015-05-16T04:44:38.813 回答
0

使用 setInexactRepeating 而不是 setRepeating

    Calendar cal= Calendar.getInstance();
    cal.set(Calendar.HOUR_OF_DAY, 9);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    PendingIntent pi = PendingIntent.getService(context, 0,
        new Intent(context, MyClass.class),PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
   am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
                            AlarmManager.INTERVAL_DAY, pi);
于 2013-07-15T04:24:36.330 回答