3

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

    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> 

问题是当用户将日期/时间从他的手机更改为未来一天或两天时,我会收到来自接收器的几个电话(取决于选择的天数)。由于我使用了一种setRepeating方法,我无法验证警报是否在正确的时间发送。

我知道这是这里描述的正常程序: “触发时间。如果您指定的触发时间是过去,警报会立即触发。” ,但我想阻止它。

我想让一个听众知道用户何时更改了他手机的日期和时间,取消安排所有警报,然后重新安排它们,如下所示:

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>

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

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

有一个更好的方法吗?

我也想把一个听众放在时钟上,把它存储起来。如果用户更改时间我应该只验证差异......但我相信我可以获得更好的方法来做到这一点(最好不涉及应用程序上的另一个听众)

4

3 回答 3

2

您不需要多个接收器来处理此问题。在中执行以下操作:

报警接收器.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>

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

希望这可以帮助。

于 2016-02-01T16:54:42.430 回答
0

这不是一个理想的解决方案,更像是一种解决方法,但它可以解决问题......

由于我找不到订购接收器的方法,我不得不在 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 年后我尝试设置小时,而且速度非常快(至少在我的设备上)

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

于 2016-04-29T12:18:39.833 回答
0

我们面临着通过删除不必要的问题解决的相同问题intent-filters

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

于 2020-04-11T02:15:31.403 回答