У меня есть приложение для напоминания, которое должно уведомлять пользователя в определенные дни в определенное время дня каждую неделю, когда оно установлено.
Переменная 'interval_value' содержит данные, для каких дней недели должен сработать будильник. Это строка из 1 и 2, где 2 означает, что будильник должен сработать в этот день недели и имеет порядок с понедельника по воскресенье.
То есть 1111112 означает, что будильник срабатывает только по воскресеньям, а 1212111 - только по вторникам и четвергам.
Но это не работает должным образом. Кто-нибудь может помочь?
public void next_alarm(Context context)
{
Integer day_sequence[] = new Integer[7];
Calendar now = Calendar.getInstance();
Calendar alarm_time = Calendar.getInstance();
String d[] = time.split(":");
alarm_time.set(Calendar.HOUR_OF_DAY, Integer.parseInt(d[0]));
alarm_time.set(Calendar.MINUTE, Integer.parseInt(d[1]));
alarm_time.set(Calendar.SECOND, 0);
switch(now.get(Calendar.DAY_OF_WEEK))
{
case Calendar.SUNDAY:
day_sequence = new Integer[]{1, 2, 3, 4, 5, 6, 7};
break;
case Calendar.MONDAY:
day_sequence = new Integer[]{2, 3, 4, 5, 6, 7, 1};
break;
case Calendar.TUESDAY:
day_sequence = new Integer[]{3, 4, 5, 6, 7, 1, 2};
break;
case Calendar.WEDNESDAY:
day_sequence = new Integer[]{4, 5, 6, 7, 1, 2, 3};
break;
case Calendar.THURSDAY:
day_sequence = new Integer[]{5, 6, 7, 1, 2, 3, 4};
break;
case Calendar.FRIDAY:
day_sequence = new Integer[]{6, 7, 1, 2, 3, 4, 5};
break;
case Calendar.SATURDAY:
day_sequence = new Integer[]{7, 1, 2, 3, 4, 5, 6};
break;
}
for(int i=0;i<7;i++) {
if(String.valueOf(frequency_value).charAt(day_sequence[i]) == '2')
{
Intent myIntent1 = new Intent(context, Task_Notification_BroadcastReceiver.class);
myIntent1.putExtra("frequency_value", frequency_value);
myIntent1.putExtra("task_time", time);
myIntent1.putExtra("task_full_text", full_text);
myIntent1.putExtra("task_text", text);
PendingIntent pendingIntent1 = PendingIntent.getBroadcast(context, Integer.parseInt(time.replace(":", "0")), myIntent1,
PendingIntent.FLAG_UPDATE_CURRENT);
alarm_time.add(Calendar.DAY_OF_YEAR, i+1);
AlarmManager alarmManager1 = (AlarmManager) context.getSystemService(ALARM_SERVICE);
assert alarmManager1 != null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
alarmManager1.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, alarm_time.getTimeInMillis(), pendingIntent1);
table_alarms_db.addAlarm(new Database_Alarms(null, alarm_time.getTimeInMillis(), time, text, full_text, frequency_value));
}
else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
alarmManager1.setExact(AlarmManager.RTC_WAKEUP, alarm_time.getTimeInMillis(), pendingIntent1);
table_alarms_db.addAlarm(new Database_Alarms(null, alarm_time.getTimeInMillis(), time, text, full_text, frequency_value));
}
else {
alarmManager1.set(AlarmManager.RTC_WAKEUP, alarm_time.getTimeInMillis(), pendingIntent1);
table_alarms_db.addAlarm(new Database_Alarms(null, alarm_time.getTimeInMillis(), time, text, full_text, frequency_value));
}
break;
}
}
}