在指定的日期和时间触发警报

Trigger an alarm in specified days and times

我有警报和通知,它们工作正常,但我需要在用户输入的特定时间和固定天数触发警报:

1.tonight 在时间选择器时间 2.tomorrow 在时间选择器时间 3.3 天后的时间选择器时间 4.14 天后的时间选择器时间 5.30 天后的时间选择器时间 6.90 天后的时间选择器时间

这是我的代码:

AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
            TimePicker timePicker1;
            timePicker1 = (TimePicker) findViewById(R.id.timePicker1);
            // ---get current date and time---
            Calendar calendar = Calendar.getInstance();


            // ---PendingIntent to launch activity when the alarm
            // triggers---
            Intent intent = new Intent(List2.this,
                    DisplayNotifications.class);
            intent.putExtra("message", esmdars);
            intent.putExtra("Title", "alarm");
            PendingIntent pendingIntent = PendingIntent.getBroadcast(
                    List2.this, 0, intent, 0);

            // ---sets the alarm to trigger---

            alarmManager.set(
                    AlarmManager.RTC_WAKEUP,
                    System.currentTimeMillis()
                            + (TimeUnit.SECONDS.toMillis(30)),
                    pendingIntent);

警报在此部分触发并显示通知,例如此处在 30 秒后激活:

alarmManager.set(
                    AlarmManager.RTC_WAKEUP,
                    System.currentTimeMillis()
                            + (TimeUnit.SECONDS.toMillis(30)),
                    pendingIntent);

如何在当天和特定时间激活?

试试这个:

Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minutes);
calendar.set(Calendar.DAY_OF_MONTH, day_of_month);
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.YEAR, year);
alarmMgr.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);