我有一个应用程序使用 AlarmManager 来安排服务以设定的频率重复,比如每小时重复一次。
从 Android 6 开始,设备将进入打盹模式,此模式会忽略唤醒锁和警报,可能会在维护窗口中运行它们。我更希望 Alarm 在计划时执行,即使设备处于低功耗空闲模式也是如此。
我知道我可以使用 setExactAndAllowWhileIdle 在打瞌睡时执行警报,但这只会执行一次。我看不到任何具有此功能但会以设定频率重复的方法。
例如,我使用下面的代码每小时发出一次警报。有没有办法使用 setExactAndAllowWhileIdle 使其重复?
// get a Calendar object with current time
Calendar cal2 = Calendar.getInstance();
// add 5 minutes to the calendar object
cal2.add(Calendar.MINUTE, 1);
Intent intentTracking = new Intent(getApplicationContext(), TrackingAlarmReceiver.class);
// In reality, you would want to have a static variable for the request code instead of 192837
PendingIntent sender3 = PendingIntent.getBroadcast(getApplicationContext(), 192839, intentTracking, PendingIntent.FLAG_UPDATE_CURRENT);
// Get the AlarmManager service
AlarmManager am3 = (AlarmManager) getSystemService(ALARM_SERVICE);
//am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
am3.setRepeating(AlarmManager.RTC_WAKEUP, cal2.getTimeInMillis(), ((Integer.parseInt(carerTrackingInteval)) * 60000 ), sender3);
最佳答案
ADM is spot-on .
作为 TrackingAlarmReceiver
工作的一部分,您调用 setExactAndAllowWhileIdle()
来安排下一点工作。
请记住,此类事件的最小粒度约为 10 分钟 IIRC,即使您获得了控制权,您也可能无法访问网络。
关于android - AlarmManager 使用 setExactAndAllowWhileIdle 所以它会重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48339484/