一次触发多个警报,而不是在特定时间触发

时间:2016-09-02 01:09:32

标签: android time alarm

我在Android中构建简单的多个警报应用程序,但我的警报同时响起。我认为millis转换有问题,但我没有得到它在这里发生的事情。在我尝试运行单个警报并且工作正常之前,我从类似的问题SO中读到一些参考,我发现如果时间毫秒在当前时间之前警报立即运行,我的代码看起来是否有同样的问题或另一次转换问题?谢谢

...
    private static AlarmManager alarmManager;
    private static ArrayList<PendingIntent> intentArray;

    public static int bellHour = 0;
    public static int bellMinute = 0;

    public static int arriveHour = 0;
    public static int arriveMinute = 0;

    public static int homeworkHour = 0;
    public static int homeworkMinute = 0;

    public static int wakeupHour = 0;
    public static int wakeupMinute = 0;

    public static int prayHour = 0;
    public static int prayMinute = 0;

    public static int workoutHour = 0;
    public static int workoutMinute = 0;

    public static int showerHour = 0;
    public static int showerMinute = 0;

    public static int breakfastHour = 0;
    public static int breakfastMinute = 0;

    public static int schoolHour = 0;
    public static int schoolMinute = 0;
...


public static void updateAlarmClock(Context context) {
...
// set variables hour and minute from sqlite
...
setupAlarm(context)
}
private static void setupAlarm(Context context) {
        cancelAlarms();

        alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        intentArray = new ArrayList<>();

        ScheduleRepository scheduleRepository = new ScheduleRepository(context);
        List<Schedule> schedules = scheduleRepository.retrieve();

        for (int i = 0; i < schedules.size(); i++) {
            Schedule schedule = schedules.get(i);
            Intent receiverIntent = new Intent(context, AlarmReceiver.class);
            receiverIntent.putExtra(Schedule.ID, schedule.getId());
            PendingIntent pendingIntent = PendingIntent.getBroadcast(context, schedule.getId(), receiverIntent, 0);
            intentArray.add(pendingIntent);
            alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, getMillisTime(schedule), AlarmManager.INTERVAL_DAY, pendingIntent);

            Log.i("Schedule Alarm", "Alarm " + schedule.getLabel() + " ID " + String.valueOf(schedule.getId()));
        }
    }

public static void cancelAlarms() {
        if (alarmManager != null && intentArray != null) {
            for (int i = 0; i < intentArray.size(); i++) {
                alarmManager.cancel(intentArray.get(i));
            }
        }
        if (AlarmReceiver.mp != null) {
            AlarmReceiver.mp.stop();
        }
    }

private static long getMillisTime(Schedule schedule) {
        int timeHour = 0;
        int timeMinute = 0;
        switch (schedule.getLabel()) {
            case Constant.ACT_HOMEWORK:
            timeHour = AlarmClock.homeworkHour;
            timeMinute = AlarmClock.homeworkMinute;
            Log.i("Schedule homework", timeHour + ":" + timeMinute);
            break;
        case Constant.ACT_WAKEUP:
            timeHour = AlarmClock.wakeupHour;
            timeMinute = AlarmClock.wakeupMinute;
            Log.i("Schedule sleep", timeHour + ":" + timeMinute);
            break;
        case Constant.ACT_PRAY:
            timeHour = AlarmClock.prayHour;
            timeMinute = AlarmClock.prayMinute;
            Log.i("Schedule pray", timeHour + ":" + timeMinute);
            break;
        case Constant.ACT_WORKOUT:
            timeHour = AlarmClock.workoutHour;
            timeMinute = AlarmClock.workoutMinute;
            Log.i("Schedule workout", timeHour + ":" + timeMinute);
            break;
        case Constant.ACT_SHOWER:
            timeHour = AlarmClock.showerHour;
            timeMinute = AlarmClock.showerMinute;
            Log.i("Schedule shower", timeHour + ":" + timeMinute);
            break;
        case Constant.ACT_BREAKFAST:
            timeHour = AlarmClock.breakfastHour;
            timeMinute = AlarmClock.breakfastMinute;
            Log.i("Schedule breakfast", timeHour + ":" + timeMinute);
            break;
        case Constant.ACT_SCHOOL:
            timeHour = AlarmClock.schoolHour;
            timeMinute = AlarmClock.schoolMinute;
            Log.i("Schedule school", timeHour + ":" + timeMinute);
            break;
        }

        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, timeHour);
        calendar.set(Calendar.MINUTE, timeMinute);

        return  calendar.getTimeInMillis();
    }

以小时和分钟为单位记录每个报警的时间

Schedule homework: 23:8
Schedule sleep: 0:8
Schedule pray: 5:18
Schedule workout: 5:28
Schedule shower: 5:43
Schedule breakfast: 5:58
Schedule school: 6:20

0 个答案:

没有答案