在特定时间使用警报管理器

Using Alarm Manager at specific time

我需要在 11:59 下午 IST 从正在设置的 AlarmManager 触发代码块。

谁能告诉我如何在 ِAndroid 中使用 AlarmManager 的示例代码?

几天来我一直在研究一些代码,但就是行不通。

这是使用AlarmManager的示例代码,您需要根据需要更改日期和时间。我今天把它放在 23:59

// Pending intent to be fired when alarm occurrs. In this case, open the AlarmActivity
Intent intent = new Intent(getApplicationContext(), AlarmActivity.class);
PendingIntent alarmIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);

// Set the alarm for today at 23:59
calendar = Calendar.getInstance(TimeZone.getTimeZone("IST"));
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);

AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP,
        calendar.getTimeInMillis(),
        alarmIntent);

在给定的日期时间 Intent 将被触发,因此您需要在 AlarmActivity 中创建逻辑。您还可以更改启动服务或触发广播消息的意图