Android - 避免用户更改时触发警报 date/time

Android - Avoid the alarm triggering when user changes date/time

我有一个应用程序可以在一周中的每一天通过 AlarmManager 中的方法 setRepeating 定期发送警报:

    Intent intent = new Intent(context, AlarmReceiver.class);
    intent.putExtra(INTENT_TAG_ALERT_ID, lastAlertId.getId()+"");
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, idRandom, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
    AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cDate.getTimeInMillis(), ((AlarmManager.INTERVAL_DAY)*7), pendingIntent);

然后在清单上定义为接收器的 AlarmReceiver 上设置警报:

    <receiver
        android:name=".manager.alarm.AlarmReceiver"
        android:exported="true">
    </receiver> 

问题是当用户将 Date/Time 从他的 phone 更改为未来一天或两天时,我会收到来自接收者的多个电话(取决于选择的天数) .由于我使用 setRepeating 方法,因此我无法验证警报是否在正确的时间发送。

我知道这是正常程序,如下所述: "A trigger time. If the trigger time you specify is in the past, the alarm triggers immediately.",但我想阻止它。

我想设置一个监听器来了解用户何时更改他的 phone 的日期和时间,以取消安排所有闹钟然后重新安排它们,如下所示:

public class DateTimeChangedBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();
    if (action.equals(Intent.ACTION_TIMEZONE_CHANGED) || action.equals(Intent.ACTION_TIME_CHANGED) || action.equals(Intent.ACTION_DATE_CHANGED)) {
        AlarmScheduler.unscheduleAllAlarms();
        //and then
        AlarmScheduler.scheduleAllAlarms();
    }
}
}

和清单:

        <receiver android:name=".manager.alarm.DateTimeChangedBroadcastReceiver"
        android:priority="1000">
        <intent-filter>
            <action android:name="android.intent.ACTION_TIMEZONE_CHANGED"/>
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.ACTION_TIME"/>
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.DATE_CHANGED" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.TIME_SET"/>
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.ACTION_TIME_CHANGED"/>
        </intent-filter>

    </receiver>

但是在这个解决方案中,时间变化的接收者是在警报的接收者之后,所以它对我没有多大用处...

有没有办法选择接收者的顺序?

有更好的方法吗?

我也考虑过让一个监听器来存储时钟时间。如果用户更改时间我应该只验证差异...但我相信我可以找到更好的方法来做到这一点(最好不涉及应用程序上的另一个听众)

您不需要多个接收者来处理这个问题。在以下位置执行以下操作:

AlarmReceiver.java

    @Override
public void onReceive(Context context, Intent intent) {


    //Handle the logics here.
    //This function is called every time alarm repeats.
    //So simply find out if you need an alarm right now and pop alarm.
}

XML

  <receiver android:name=".manager.alarm.AlarmReceiver"
         android:exported="true">
        <intent-filter>
            <action android:name="android.intent.ACTION_TIMEZONE_CHANGED"/>
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.ACTION_TIME"/>
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.DATE_CHANGED" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.TIME_SET"/>
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.ACTION_TIME_CHANGED"/>
        </intent-filter>

    </receiver>

根据您的时间间隔或日期更改等调用警报接收器...然后您可以检测导致警报接收器被触发的原因并采取行动。为此目的使用多个接收器只是矫枉过正。

希望对您有所帮助。

这不是一个理想的解决方案,更像是一种解决方法,但它确实有效...

由于找不到订购接收器的方法,我被迫在 Alarm Receiver 中找到解决方案...所以我创建了一个方法来了解 Alarm 是否有效...有点大,但它处理日常和定期警报...... 这是代码:

    public boolean isReceiveAlarmValid (AlertData alertData, AlarmData alarmData){
    if ((alertData != null) && (alarmData != null)) {

        Calendar caInitialDate = Calendar.getInstance();
        //Set beginning of counting to alarm starting date
        caInitialDate.set(Calendar.YEAR, alarmData.getTimeAlarmYear());
        caInitialDate.set(Calendar.MONTH, (alarmData.getTimeAlarmMonth()) - 1);
        caInitialDate.set(Calendar.DAY_OF_MONTH, alarmData.getTimeAlarmDay());
        caInitialDate.set(Calendar.HOUR_OF_DAY, alarmData.getTimeAlarmHour());
        caInitialDate.set(Calendar.MINUTE, alarmData.getTimeAlarmMinute());
        caInitialDate.set(Calendar.SECOND, alarmData.getTimeAlarmSeconds());

        caInitialDate.add(Calendar.MINUTE, alertData.getRescheduleCounter() * GeneralData.getInstance().getAlarmReschedulePeriodicity());

        alarmData.setTimeAlarm(GlobalFunctions.generateStringDateFromCalendar(caInitialDate));

        //Initialization
        int sizeAux = 7;
        String auxDateComp = "";
        String auxDate = null;
        String[] auxDateArray = new String[7];

        Calendar calendar = Calendar.getInstance();
        int todayDay = calendar.get(Calendar.DAY_OF_WEEK);

        if (Consts.FREQUENCY_ONCE == alarmData.getAlarmFrequency()) {
            //Alarm Once
            auxDate = String.format(Locale.getDefault(), "%04d", alarmData.getTimeAlarmYear()) + "-" +
                    String.format(Locale.getDefault(), "%02d", alarmData.getTimeAlarmMonth()) + "-" +
                    String.format(Locale.getDefault(), "%02d", alarmData.getTimeAlarmDay()) + " " +
                    String.format(Locale.getDefault(), "%02d", alarmData.getTimeAlarmHour()) + ":" +
                    String.format(Locale.getDefault(), "%02d", alarmData.getTimeAlarmMinute()) + ":00";

            auxDateComp = auxDate;

        } else if (Consts.FREQUENCY_DAILY == alarmData.getAlarmFrequency()) {
            //Alarm Daily

            //Cycle to know what a specific day we are gonna compare on the week
            dayCicle:
            for (int i = 0; i < 7; i++) {

                int auxDay = todayDay + i;
                if (auxDay > 7)
                    auxDay = auxDay - 7;

                auxDateArray = new String[7];

                if (((alarmData.isRepeatMonday()) && (auxDay == dayMon)) ||
                        ((alarmData.isRepeatTuesday()) && (auxDay == dayTue)) ||
                        ((alarmData.isRepeatWednesday()) && (auxDay == dayWed)) ||
                        ((alarmData.isRepeatThursday()) && (auxDay == dayThu)) ||
                        ((alarmData.isRepeatFriday()) && (auxDay == dayFri)) ||
                        ((alarmData.isRepeatSaturday()) && (auxDay == daySat)) ||
                        ((alarmData.isRepeatSunday()) && (auxDay == daySun))) {

                    int dayPeriodic = auxDay;
                    boolean[] isRepeatElements = new boolean[7];
                    isRepeatElements[Consts.ANDROID_DAY_OF_THE_WEEK_SUNDAY] = alarmData.isRepeatSunday();
                    isRepeatElements[Consts.ANDROID_DAY_OF_THE_WEEK_MONDAY] = alarmData.isRepeatMonday();
                    isRepeatElements[Consts.ANDROID_DAY_OF_THE_WEEK_TUESDAY] = alarmData.isRepeatTuesday();
                    isRepeatElements[Consts.ANDROID_DAY_OF_THE_WEEK_WEDNESDAY] = alarmData.isRepeatWednesday();
                    isRepeatElements[Consts.ANDROID_DAY_OF_THE_WEEK_THURSDAY] = alarmData.isRepeatThursday();
                    isRepeatElements[Consts.ANDROID_DAY_OF_THE_WEEK_FRIDAY] = alarmData.isRepeatFriday();
                    isRepeatElements[Consts.ANDROID_DAY_OF_THE_WEEK_SATURDAY] = alarmData.isRepeatSaturday();


                    int daysCount = i;

                    Calendar caTimeStartAlarm = Calendar.getInstance();
                    Calendar caNow = Calendar.getInstance();
                    //Set beginning of counting to alarm starting date
                    caTimeStartAlarm.set(Calendar.YEAR, alarmData.getTimeAlarmYear());
                    caTimeStartAlarm.set(Calendar.MONTH, (alarmData.getTimeAlarmMonth()) - 1);
                    caTimeStartAlarm.set(Calendar.DAY_OF_MONTH, alarmData.getTimeAlarmDay());
                    caTimeStartAlarm.set(Calendar.HOUR_OF_DAY, alarmData.getTimeAlarmHour());
                    caTimeStartAlarm.set(Calendar.MINUTE, alarmData.getTimeAlarmMinute());
                    caTimeStartAlarm.set(Calendar.SECOND, alarmData.getTimeAlarmSeconds());

                    Calendar calInitial = null;

                    //If time now is after starting time, the base date is the now date
                    if (caTimeStartAlarm.after(caNow)) {
                        calInitial = (Calendar) caTimeStartAlarm.clone();
                    } else {
                        calInitial = (Calendar) caNow.clone();
                    }


                    //cycle that increase days adding to next alarms
                    for (int z = 0; z < 7; z++) {

                        //only chosen periodic daily days
                        if (isRepeatElements[dayPeriodic - 1]) {

                            Calendar caPossibleAlarm = (Calendar) calInitial.clone();
                            //Add daily day
                            caPossibleAlarm.set(Calendar.DAY_OF_MONTH, caPossibleAlarm.get(Calendar.DAY_OF_MONTH) + daysCount);
                            caPossibleAlarm.set(Calendar.HOUR_OF_DAY, alarmData.getTimeAlarmHour());
                            caPossibleAlarm.set(Calendar.MINUTE, alarmData.getTimeAlarmMinute());
                            caPossibleAlarm.set(Calendar.SECOND, alarmData.getTimeAlarmSeconds());

                            //add a few seconds of margin
                            caPossibleAlarm.add(Calendar.SECOND, ALARM_RECEIVE_TIME_MARGIN);

                            if (!(calInitial.after(caPossibleAlarm))) {
                                //If now is not after (before or equal) next alarm, then it can show

                                String yearAux = String.format(Locale.getDefault(), "%04d", caPossibleAlarm.get(Calendar.YEAR));
                                String monthAux = String.format(Locale.getDefault(), "%02d", caPossibleAlarm.get(Calendar.MONTH) + 1);
                                String dayAux = String.format(Locale.getDefault(), "%02d", caPossibleAlarm.get(Calendar.DAY_OF_MONTH));
                                auxDate = yearAux + "-" + monthAux + "-" + dayAux + " " +
                                        String.format(Locale.getDefault(), "%02d", alarmData.getTimeAlarmHour()) + ":" +
                                        String.format(Locale.getDefault(), "%02d", alarmData.getTimeAlarmMinute()) + ":00";

                                auxDateComp = auxDate;

                            }

                        }

                        if ("".equals(auxDateComp)) {

                            dayPeriodic++;
                            if (dayPeriodic > 7)
                                dayPeriodic = dayPeriodic - 7;

                            daysCount++;
                        } else {
                            //it has found a value
                            break dayCicle;
                        }
                    }

                    break dayCicle;

                }

            }

        } else {
            //Periodic Weekly / Monthly

            auxDate = String.format(Locale.getDefault(), "%04d", alarmData.getTimeAlarmYear()) + "-" +
                    String.format(Locale.getDefault(), "%02d", alarmData.getTimeAlarmMonth()) + "-" +
                    String.format(Locale.getDefault(), "%02d", alarmData.getTimeAlarmDay()) + " " +
                    String.format(Locale.getDefault(), "%02d", alarmData.getTimeAlarmHour()) + ":" +
                    String.format(Locale.getDefault(), "%02d", alarmData.getTimeAlarmMinute()) + ":00";

            Calendar caNow = Calendar.getInstance();
            Calendar caAlarmTime = Calendar.getInstance();

            caAlarmTime.set(Calendar.YEAR, alarmData.getTimeAlarmYear());
            caAlarmTime.set(Calendar.MONTH, alarmData.getTimeAlarmMonth());
            caAlarmTime.set(Calendar.DAY_OF_MONTH, alarmData.getTimeAlarmDay());
            caAlarmTime.set(Calendar.HOUR_OF_DAY, alarmData.getTimeAlarmHour());
            caAlarmTime.set(Calendar.MINUTE, alarmData.getTimeAlarmMinute());

            if (caAlarmTime.after(caNow)) {
                //next date is from the alarm
                auxDate = String.format(Locale.getDefault(), "%04d", alarmData.getTimeAlarmYear()) + "-" +
                        String.format(Locale.getDefault(), "%02d", alarmData.getTimeAlarmMonth()) + "-" +
                        String.format(Locale.getDefault(), "%02d", alarmData.getTimeAlarmDay()) + " " +
                        String.format(Locale.getDefault(), "%02d", alarmData.getTimeAlarmHour()) + ":" +
                        String.format(Locale.getDefault(), "%02d", alarmData.getTimeAlarmMinute()) + ":00";
            } else {
                //It needs to check when is the next possible alarm
                boolean foundNextDate = false;

                foundNextDateCycle:
                while (foundNextDate == false) {

                    if (Consts.FREQUENCY_WEEKLY == alarmData.getAlarmFrequency())
                        caAlarmTime.add(Calendar.DAY_OF_MONTH, 7);
                    else if (Consts.FREQUENCY_MONTHLY == alarmData.getAlarmFrequency())
                        caAlarmTime.add(Calendar.MONTH, 1);
                    else
                        caAlarmTime.add(Calendar.YEAR, 1);

                    try {
                        if (caAlarmTime.after(caNow))
                            foundNextDate = true;
                    } catch (Exception e) {
                        break foundNextDateCycle;
                    }

                }


                auxDate = String.format(Locale.getDefault(), "%04d", caAlarmTime.get(Calendar.YEAR)) + "-" +
                        String.format(Locale.getDefault(), "%02d", caAlarmTime.get(Calendar.MONTH) + 1) + "-" +
                        String.format(Locale.getDefault(), "%02d", caAlarmTime.get(Calendar.DAY_OF_MONTH)) + " " +
                        String.format(Locale.getDefault(), "%02d", caAlarmTime.get(Calendar.HOUR_OF_DAY)) + ":" +
                        String.format(Locale.getDefault(), "%02d", caAlarmTime.get(Calendar.MINUTE)) + ":00";

            }

            auxDateComp = auxDate;
        }




        if("".equals(auxDateComp))
            return false;


        Calendar calendarNow = Calendar.getInstance();
        Calendar calendarBegin = Calendar.getInstance();
        Calendar calendarFisnish = Calendar.getInstance();


        int marginMinutes = 1; //1 minute of margin


        calendarBegin.set(Calendar.YEAR, Integer.parseInt(GlobalFunctions.formateDateFromString("yyyy-MM-dd HH:mm:ss", "yyyy", auxDateComp)));
        calendarBegin.set(Calendar.MONTH, (Integer.parseInt(GlobalFunctions.formateDateFromString("yyyy-MM-dd HH:mm:ss", "MM", auxDateComp))) - 1);
        calendarBegin.set(Calendar.DAY_OF_MONTH, Integer.parseInt(GlobalFunctions.formateDateFromString("yyyy-MM-dd HH:mm:ss", "dd", auxDateComp)));
        calendarBegin.set(Calendar.HOUR_OF_DAY, Integer.parseInt(GlobalFunctions.formateDateFromString("yyyy-MM-dd HH:mm:ss", "HH", auxDateComp)));
        calendarBegin.set(Calendar.MINUTE, Integer.parseInt(GlobalFunctions.formateDateFromString("yyyy-MM-dd HH:mm:ss", "mm", auxDateComp)));
        calendarBegin.set(Calendar.SECOND, Integer.parseInt(GlobalFunctions.formateDateFromString("yyyy-MM-dd HH:mm:ss", "ss", auxDateComp)));


        calendarFisnish = (Calendar) calendarBegin.clone();

        calendarBegin.set(Calendar.MINUTE, calendarBegin.get(Calendar.MINUTE) - marginMinutes);
        calendarFisnish.set(Calendar.MINUTE, calendarFisnish.get(Calendar.MINUTE) + marginMinutes);

        if (calendarNow.after(calendarBegin) && calendarNow.before(calendarFisnish)) {
            Log.e(GlobalFunctions.getTag(), "Gonna show alarm!");
            return true;
        } else {
            Log.e(GlobalFunctions.getTag(), "NOT Gonna show alarm!");
            return false;
        }


    } else{
        //alarmData or alertData are NULL
        return false;
    }

}

在此之后我还检查我收到的警报数组是否具有相同 ID 的值,然后只显示其中一个(只是为了确保没有剩余)

我在 2 年后尝试设置小时,它非常快(至少在我的设备上)

同样,这不是一个理想的解决方案,如果有人会找到更好的解决方案,请post它

我们遇到了同样的问题,通过删除不必要的 intent-filters

来自 android 清单文件中的接收方。