我需要在 IST 晚上 11:59 从正在设置的 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 中创建逻辑。您还可以更改启动服务或触发广播消息的 Intent
关于java - 在特定时间使用警报管理器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61042036/