我正在尝试使用 AlarmManager 在我的应用程序中安排延迟检查。 (具体来说,用户接近某个位置 N 分钟后,我想检查他们是否仍然在那里,如果是,则向他们发送通知。)
我通过检查他们是否已进入我的位置更新接收器中的区域来实现此目的,如果他们已进入,则按如下方式进行安排:
Intent geofenceIntent = new Intent(context, GeofenceReceiver.class)
// ...intent contents not important...
PendingIntent pi = PendingIntent.getBroadcast(context, 0, geofenceIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar c = Calendar.getInstance();
c.add(Calendar.SECOND, getGeofenceDelaySeconds());
AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pi);
Log.v("Scheduling notification check for "+c.getTime());
当电池电量较高时(例如 50%),一切都会变得非常顺利。但是当它很低时(比如 10%),我会得到位置更新,他们看似成功地安排了警报,但该警报实际上从未触发!
是什么赋予了? Android 在尝试节省电量时是否会停止发送某些类型的更新?我该如何解决这个问题(在延迟期间没有实际保持我的应用程序处于活动状态,这会导致明显的电池寿命问题)?
原来这和实时时钟的使用有关。
虽然我找不到它引用的文档(它不在 AlarmManager 中),这个 StackOverflow 答案 https://stackoverflow.com/a/15400014/225176建议AlarmManager.RTC_WAKEUP
如果手机处于省电模式,则不会触发警报. AlarmManager.ELAPSED_REALTIME_WAKEUP
警报似乎没有遇到这个问题,所以我能够通过切换到来解决这个问题:
Intent geofenceIntent = new Intent(context, GeofenceReceiver.class)
// ...intent contents not important...
PendingIntent pi = PendingIntent.getBroadcast(context, 0, geofenceIntent, PendingIntent.FLAG_UPDATE_CURRENT);
long millis = SystemClock.elapsedRealtime() + 1000 * getGeofenceDelaySeconds();
AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, millis, pi);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)