Android AlarmManager赢得';t在特定时间触发

Android AlarmManager赢得';t在特定时间触发,android,alarmmanager,Android,Alarmmanager,我对经理有意见。我可以用这个代码设置闹钟 private void setAlarm(long when) { Intent intent = new Intent(NoteActivity.this, AlarmReceiver.class); intent.putExtra("ID", note.getId()); AlarmManager manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);

我对经理有意见。我可以用这个代码设置闹钟

private void setAlarm(long when) {
    Intent intent = new Intent(NoteActivity.this, AlarmReceiver.class);
    intent.putExtra("ID", note.getId());
    AlarmManager manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
    manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, when, PendingIntent.getBroadcast(NoteActivity.this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT));
    Toast.makeText(getApplicationContext(),"Reminder set up", Toast.LENGTH_SHORT).show();
}
Calendar calendar = Calendar.getInstance();
                    calendar.setTime(date);
                    long selectedDate = date.getTime();
                    long timeSince1970 = System.currentTimeMillis();
                    long timeForAlarm = selectedDate - timeSince1970;

                    Intent intent = new Intent(NoteActivity.this, AlarmReceiver.class);
                    intent.putExtra("ID", note.getId());
                    AlarmManager manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
                    manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, timeForAlarm, PendingIntent.getBroadcast(NoteActivity.this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT));
                    Toast.makeText(getApplicationContext(),"Reminder set for "+calendar.getTime().toString(), Toast.LENGTH_SHORT).show();
如果我在=5*1000时设置
long,则此代码运行良好\\例如5秒后
,但如果我使用此代码

private void setAlarm(long when) {
    Intent intent = new Intent(NoteActivity.this, AlarmReceiver.class);
    intent.putExtra("ID", note.getId());
    AlarmManager manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
    manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, when, PendingIntent.getBroadcast(NoteActivity.this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT));
    Toast.makeText(getApplicationContext(),"Reminder set up", Toast.LENGTH_SHORT).show();
}
Calendar calendar = Calendar.getInstance();
                    calendar.setTime(date);
                    long selectedDate = date.getTime();
                    long timeSince1970 = System.currentTimeMillis();
                    long timeForAlarm = selectedDate - timeSince1970;

                    Intent intent = new Intent(NoteActivity.this, AlarmReceiver.class);
                    intent.putExtra("ID", note.getId());
                    AlarmManager manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
                    manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, timeForAlarm, PendingIntent.getBroadcast(NoteActivity.this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT));
                    Toast.makeText(getApplicationContext(),"Reminder set for "+calendar.getTime().toString(), Toast.LENGTH_SHORT).show();
我的闹钟在2秒后触发。我做错了什么?:/ 我尝试了
AlarmManager.eassed\u REALTIME\u WAKEUP
AlarmManager.RTC\u WAKEUP
,但没有任何更改


请不要检查我的问题是否重复。我没有找到解决问题的方法。

假设您在Android Studio中工作(如果没有-您必须切换),请在文本指针位于
set
方法上时单击F1,然后阅读方法说明

注意:从API 19开始,传递给此方法的触发时间为 视为不准确:在此之前不会发出警报, 但可能会推迟一段时间交付。操作系统将使用 此策略旨在跨整个系统“批量”报警 系统,最大限度地减少设备需要“唤醒”的次数 并尽量减少电池的使用。通常情况下,警报安排在近期 只要警报安排在较远的时间内,未来将不会延迟 未来


使用setExact代替set

 manager.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, timeForAlarm, PendingIntent.getBroadcast(NoteActivity.this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT));

(提示:告诉社区不要关闭某个复制品,如果它是真正的复制品,这不会改变任何事情。最多只能帮助它有更多的目标来关闭。):-\@Jonaddas确实如此。hahaSo,有没有办法安排我的闹钟?我应该使用权限设置报警或类似的东西吗?伙计,我已经给了你一个链接,并告诉你如何在AS中使用文档。你需要做的只是阅读。这里从doc复制过去:
如果您的应用程序有很强的排序需求,那么您可以使用其他API来获得必要的行为;请参阅setWindow(int,long,long,pendingent)和setExact(int,long,pendingent)。
不起作用:/t仍然存在相同的问题。警报在2秒后触发。