如何停止即将到来的警报触发

时间:2018-12-09 08:09:09

标签: android alarmmanager android-alarms repeatingalarm

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;
    }

}

这是我要设置,取消和检查是否从活动中设置了警报的警报类别。 我面临的问题是,设置方法仅接受DAY,HOUR和Minute,并且它每周(在7天之后)重复一次,因此,每当我设置的警报不同于今天时,警报都会立即触发,但我不希望这样即将发生。我想在特定的日期,时间和分钟触发警报。

示例场景,如果我为明天(星期一,10:10)设置了警报,则警报将立即触发。

1 个答案:

答案 0 :(得分:0)

您可以尝试:

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);
...