Как остановить будущий триггер тревоги - PullRequest
0 голосов
/ 09 декабря 2018
public class MyAlarm implements IAlarm {
    AlarmManager manager;
    private Context context;

    public MyAlarm(Context context) {
        this.context = context;
        manager = (AlarmManager) context.getSystemService(ALARM_SERVICE);
    }

    public void set(int notificationId, int day, int hour, int minute, String title, String description) {
        //Set up the Notification Broadcast Intent
        Intent notifyIntent = new Intent(context, AlarmReceiver.class);
        notifyIntent.putExtra("title", title);
        notifyIntent.putExtra("description", description);

        //Set up the PendingIntent for the AlarmManager
        final PendingIntent notifyPendingIntent = PendingIntent.getBroadcast
                (context, notificationId, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.set(Calendar.DAY_OF_WEEK, day);
        calendar.set(Calendar.HOUR_OF_DAY, hour);
        calendar.set(Calendar.MINUTE, minute);

        long triggerTime = calendar.getTimeInMillis();
        Log.v("Alarm", triggerTime+"");

        // to have an interval with in a week multiply INTERVAL_DAY by 7
        long repeatInterval = AlarmManager.INTERVAL_DAY * 7;

        manager.setRepeating(AlarmManager.RTC_WAKEUP,
                triggerTime, repeatInterval, notifyPendingIntent);
        Log.v("Alarm"," Alarm is setted");

    }

    public void cancel(int notificationId, String title, String description) {
        Intent notifyIntent = new Intent(context, AlarmReceiver.class);
        notifyIntent.putExtra("title", title);
        notifyIntent.putExtra("description", description);

        final PendingIntent notifyPendingIntent = PendingIntent.getBroadcast
                (context, notificationId, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        //Cancel the alarm and notification if the alarm is turned off
        Log.v("Alarm", notifyPendingIntent.toString());
        manager.cancel(notifyPendingIntent);
        notifyPendingIntent.cancel();
    }

    public boolean isAlarmSet(int notificationId, String title, String description) {
        Intent notifyIntent = new Intent(context, AlarmReceiver.class);
        notifyIntent.putExtra("title", title);
        notifyIntent.putExtra("description", description);

        boolean alarmUp = (PendingIntent.getBroadcast(context, notificationId, notifyIntent,
                PendingIntent.FLAG_NO_CREATE) != null);
        return alarmUp;
    }

}

Вот мой класс будильника, который нужно установить, отменить и проверить, установлен ли будильник из действия.Проблема, с которой я столкнулся, заключается в том, что метод set принимает только ДЕНЬ, ЧАС и Минуту, и он повторяется каждую неделю (через 7 дней), поэтому всякий раз, когда я устанавливаю будильник, отличный от сегодняшнего, будильник срабатывает немедленно, но я этого не хочупроизойдет.Я хочу, чтобы сигнал тревоги срабатывал в определенный день, час и минуту.

Пример сценария, если я установил будильник на завтра (понедельник, 10:10), сигнал тревоги сработает немедленно.

1 Ответ

0 голосов
/ 09 декабря 2018

Вы можете попробовать:

Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());

//set Calendar to next week
int i = calendar.get(Calendar.WEEK_OF_MONTH);
calendar.set(Calendar.WEEK_OF_MONTH, ++i); 

calendar.set(Calendar.DAY_OF_WEEK, day);
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minute);
...
...