Android AlarmManager执行时间问题

发布于 2024-11-03 16:53:37 字数 869 浏览 7 评论 0原文

我设置了一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暗藏城府 2024-11-10 16:53:37

您可以创建一个派生自 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文