即使设备处于打ze模式,如何也可以发出重复警报

问题描述 投票:0回答:1

我已经搜索了,但是没有找到解决方案。我的应用程序需要在用户决定的不同时间显示每日通知,但是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()与重复警报一起使用,那么您希望采用哪种解决方案进行重复警报,它必须可以工作。

android alarmmanager repeatingalarm setalarmclock
1个回答
0
投票

您必须使用:

if( Build.VERSION.SDK_INT >= 23 ){
    alarmManager.setExactAndAllowWhileIdle( ... );
}
© www.soinside.com 2019 - 2024. All rights reserved.