【问题标题】:How to stop an upcoming alarm trigger如何停止即将到来的警报触发
【发布时间】:2018-12-09 08:09:09
【问题描述】:
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 方法只接受 DAY、HOUR 和 Minute,它每周重复一次(7 天后),所以每当我设置与今天不同的闹钟时,闹钟会立即触发,但我不希望那样即将发生。我希望在特定的日期、小时和分钟触发警报。

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

【问题讨论】:

  • 不是很清楚,你设置的闹钟是周一触发的,如果今天是周日,为什么会触发?
  • 这就是问题@navylover。星期一 -----> 星期日 -> 星期一,我认为它使用上一个星期一,如何使它使用下一个星期一。

标签: android alarmmanager android-alarms repeatingalarm


【解决方案1】:

你可以试试:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多