Android 如何使重复报警在设备处于打盹模式时仍能正常工作

Android 如何使重复报警在设备处于打盹模式时仍能正常工作,android,alarmmanager,repeatingalarm,setalarmclock,Android,Alarmmanager,Repeatingalarm,Setalarmclock,我到处寻找,但没有找到解决办法。我的应用程序需要在用户决定的不同时间显示每日通知,但alarmmanager无法正常工作,如果我为接下来的几分钟设置时间,它将显示通知,但大多数情况下,当我不使用设备时,它无法工作。 这就是我一直试图设置的重复警报 Intent intent = new Intent(Reminder.this, AlarmReceiverDaily.class); PendingIntent pendingIntent = PendingInte

我到处寻找,但没有找到解决办法。我的应用程序需要在用户决定的不同时间显示每日通知,但alarmmanager无法正常工作,如果我为接下来的几分钟设置时间,它将显示通知,但大多数情况下,当我不使用设备时,它无法工作。 这就是我一直试图设置的重复警报

        Intent intent = new Intent(Reminder.this, AlarmReceiverDaily.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(Reminder.this, 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager alarmManager = (AlarmManager) Reminder.this.getSystemService(ALARM_SERVICE);
        alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), 24 * 60 * 60 * 1000, pendingIntent);
如何通过以下代码设置重复报警,即使我的设备处于睡眠模式

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
        alarmManager.setAlarmClock(new AlarmManager.AlarmClockInfo(targetCal.getTimeInMillis(),pendingIntent),pendingIntent);
    else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
        alarmManager.setExact(AlarmManager.RTC, targetCal.getTimeInMillis(), pendingIntent);
    else
        alarmManager.set(AlarmManager.RTC, targetCal.getTimeInMillis(), pendingIntent);
如果我不能将setAlarmClock()与重复报警一起使用,那么您更喜欢哪种解决方案来制作重复报警,并且它必须工作。

您必须使用:

if( Build.VERSION.SDK_INT >= 23 ){
    alarmManager.setExactAndAllowWhileIdle( ... );
}

但是我怎么能每天重复呢?就像我在broadcast Receiver中使用setInexactRepeating()一样,您可以启动新的AlarmManager(它是一个链)等。