Android AlarmManager.RTC不工作?

Android AlarmManager.RTC不工作?,android,Android,我在ApiDemo中稍微更改了AlarmController.java,因此我希望使用AlarmManager.RTC在手机处于睡眠状态时不会发出警报 Intent intent = new Intent(AlarmController.this, RepeatingAlarm.class); PendingIntent sender = PendingIntent.getBroadcast(AlarmController.this,

我在ApiDemo中稍微更改了AlarmController.java,因此我希望使用AlarmManager.RTC在手机处于睡眠状态时不会发出警报

        Intent intent = new Intent(AlarmController.this, RepeatingAlarm.class);
        PendingIntent sender = PendingIntent.getBroadcast(AlarmController.this,
                0, intent, 0);

        // We want the alarm to go off 30 seconds from now.
        long firstTime = SystemClock.elapsedRealtime();
        firstTime += 15*1000;

        // Schedule the alarm!
        AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
        am.setRepeating(AlarmManager.RTC, //AlarmManager.ELAPSED_REALTIME_WAKEUP,
                        firstTime, 15*1000, sender);
接收器代码如下所示:

public class RepeatingAlarm extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        Log.d("DEBUG", "In RepeatingAlarm.onReceive, intent=" + intent);
        Toast.makeText(context, R.string.repeating_received, Toast.LENGTH_SHORT).show();
    }
}
我运行了修改后的应用程序,但在手机进入睡眠状态(屏幕为黑色)后,我仍然看到许多日志消息,如下所示:

D/DEBUG(1390):在RepeatingAlarm.onReceive中,intent=intent{flg=0x4 cmp=com.example.android.api/.app.RepeatingAlarm(具有额外功能)}

这意味着AlarmManager.RTC标志不起作用。有人能告诉我为什么吗


谢谢。

由于您使用elapsedRealtime来获取报警开始时间,我认为您需要使用该标志而不是RTC标志

我的猜测是,报警管理器认为它错过了大量报警,因为您使用的是RTC标志,这意味着报警管理器希望您从1970年1月1日起发送一个以毫秒为单位的时间值,但实际上,您发送的是设备启动后经过的毫秒,这将是一个小得多的数字


如果使用RTC标志,则需要使用System.currentTimeMillis()或从Java日期或日历对象获取以毫秒为单位的时间。如果使用已用实时标志,则需要使用SystemClock.elapsedRealtime()。

谢谢您的评论。我可能知道为什么设备没有进入睡眠状态。警报发送广播的频率太高(每15秒一次),使设备处于唤醒状态。mbaird,您关于使用System.currentTimeMillis()的评论是正确的。谢谢