我有一个应用程序,通过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
方法,因此我无法验证警报是否在合适的时间发送。
我知道这是正常程序,如下所述: "A trigger time. If the trigger time you specify is in the past, the alarm triggers immediately.",但我想阻止它。
我想让听众知道用户何时更改手机的日期和时间,取消预定所有闹钟,然后重新安排,如下所示:
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>
但是在这个解决方案中,时间变化的接收器来自警报器的接收器之后,所以它并没有为我服务......
有没有办法选择接收器的顺序?
有更好的方法吗?
我也想把听众放在时间上来存储它。如果用户改变小时我应该只验证差异...但我相信我可以得到一个更好的方法来做到这一点(优先不涉及应用程序上的另一个监听器)
答案 0 :(得分:1)
你不需要多个接收器来处理这个问题。请执行以下操作:
<强> AlarmReceiver.java 强>
conn.getErrorStream()
<强> XML 强>
@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.
}
根据您的间隔或日期更改等来调用警报接收器......然后您可以检测是什么原因导致警报接收器被触发并对其采取行动。为此目的,使用多个接收器实在是太过分了。
希望这有帮助。
答案 1 :(得分:0)
这不是一个理想的解决方案,更像是一种解决方法,但它可以解决问题......
由于我找不到订购接收器的方法,我被迫在报警接收器内找到一个解决方案...所以我创建了一个方法来知道报警是否有效......它有点大,但它处理日常和定期警报...... 这是代码:
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年后设置小时,而且速度非常快(至少在我的设备上)
同样,这不是一个理想的解决方案,如果有人能找到更好的解决方案,请发布
答案 2 :(得分:0)
我们正面临通过删除不必要的intent-filters
来自Android清单文件中的接收器。