Android AlarmManager执行时间问题
我设置了一个 AlarmManager,它会在一定时间后重复发出警报。我为此使用了以下代码。
Intent intent = new Intent(ViewDoughnut.this, NotificationMessage.class);
PendingIntent sender = PendingIntent.getBroadcast(ViewDoughnut.this, 0, intent,PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP,nextAlarmTime, alarmInterval, sender);
现在,为了在闹钟时间执行正确的工作,我创建了以下扩展 BroadcastReceiver 的类。现在我需要在该类中执行警报工作的时间(以毫秒为单位)。如何获得?
public class NotificationMessage extends BroadcastReceiver {
// Display an alert that we've received a message.
// @Override
public void onReceive(Context context, Intent intent) {
// here I need the time when the alarm should execute.
}
}
在这里我想补充一点,系统时间对我来说不起作用,因为如果设备在闹钟时间关闭,它会在该时间之后设备打开时执行。但我需要它应该执行的时间。
I have set a AlarmManager which will give alarm repeatedly after certain time. I used following code for that.
Intent intent = new Intent(ViewDoughnut.this, NotificationMessage.class);
PendingIntent sender = PendingIntent.getBroadcast(ViewDoughnut.this, 0, intent,PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP,nextAlarmTime, alarmInterval, sender);
Now to execute proper work at the alarm time I have created the following class extending BroadcastReceiver. Now I need the time in millisecond when the Alarm work should execute in that class. How to get it?
public class NotificationMessage extends BroadcastReceiver {
// Display an alert that we've received a message.
// @Override
public void onReceive(Context context, Intent intent) {
// here I need the time when the alarm should execute.
}
}
Here I like to add, system time is not working for me, because if the device is switch off at the alarm time, it execute that when the device is on after that time. But I need the time when it should execute.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以创建一个派生自 Application 的类,其中包含所有全局变量。然后只需设置一个长变量来保存初始化警报之前的时间
You could create a class that derives from Application which holds all global variables. Then just set a long variable to hold the time before initialising the alarm