简体   繁体   English

Android AlarmManager执行时间问题

[英]Android AlarmManager execution time problem

I have set a AlarmManager which will give alarm repeatedly after certain time. 我设置了一个AlarmManager,它将在一定时间后反复发出警报。 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. 现在,为了在警报时间执行适当的工作,我创建了以下类,扩展了BroadcastReceiver。 Now I need the time in millisecond when the Alarm work should execute in that class. 现在,我需要在该类中执行Alarm工作的时间(以毫秒为单位)。 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. 但是我需要时间来执行它。

You could create a class that derives from Application which holds all global variables. 您可以创建一个从Application派生的类,其中包含所有全局变量。 Then just set a long variable to hold the time before initialising the alarm 然后只需设置一个长变量来保存时间,然后再初始化警报

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM